void charge() { numShots = 0; if (chargedShot==null) { charge_anim.SetBool("charge", true); chargedShot = Instantiate (gun.bullet, gun.firingLocation.position, Quaternion.identity) as Rigidbody2D; chargedShot.gameObject.SetActive(true); chargedCollider = chargedShot.GetComponent<CircleCollider2D>(); chargedCollider.enabled = false;// disable the collider first script = chargedShot.GetComponent<BulletScript>(); // assign the script if (master.shipMode){ chargedShot.rotation = 90; // 270 degrees means facing north } else { master.setCharge(1f); // set 'charging' layer to activate } chargedShot.GetComponent<SpriteRenderer>().enabled = false; } else { //if (chargeLevel<=totalCharge) Vector3 size = chargedShot.transform.localScale; chargedShot.transform.localScale = new Vector3(size.x*1.5f, size.y*1.5f, 1f); script.damage = script.damage*2; } // make the charged shot stronger chargeLevel++; }
IEnumerator Warp(Rigidbody2D warper) { Vector2 newVel = warper.velocity; newVel.Normalize(); newVel = warper.velocity.magnitude * link.transform.up; warper.GetComponent<SpriteRenderer>().enabled = false; warper.constraints = RigidbodyConstraints2D.FreezeAll; yield return new WaitForSeconds(warpTime); warper.GetComponent<SpriteRenderer>().enabled = true; warper.constraints = RigidbodyConstraints2D.None; warper.transform.position = link.transform.position; warper.velocity = newVel; warper.transform.rotation = link.transform.rotation; }
// Use this for initialization void Start () { RigidFrontWheel = frontWheel.GetComponent<Rigidbody2D> (); RigidRearWheel = rearWheel.GetComponent<Rigidbody2D> (); WjFrontWheel = RigidFrontWheel.GetComponent<WheelJoint2D> (); WjRearWheel = RigidRearWheel.GetComponent<WheelJoint2D> (); }
void DetachChunk(Rigidbody2D chunk) { chunk.isKinematic = false; chunk.GetComponent<Collider2D>().enabled = true; chunk.angularVelocity = angularSpeed.Random() * Utils.RandomSign(); chunk.velocity = new Vector2(horizontalForce.Random() * Utils.RandomSign(), verticalForce.Random()); }
// Update is called once per frame void Update () { //RaycastHit2D hit = Physics2D.Raycast (transform.position, transform.up); //Debug.DrawLine (transform.position, hit.point); //laserHit.position = hit.point; if (stateMachine.getMovement() == CentralStateScript.Movement.Flight) { lineRenderer.enabled = false; } else if (stateMachine.getMovement() == CentralStateScript.Movement.Orbit) { lineRenderer.enabled = true; } if (UFO == null) { UFO = GameObject.FindGameObjectWithTag ("Player").GetComponent<Rigidbody2D> (); if (UFO == null) { return; } playerController = UFO.GetComponent<NewPlayerController> (); } transform.position = UFO.position + playerController.BurstDirecton; laserHit.position = (Vector2)transform.position + playerController.BurstDirecton / 10; lineRenderer.SetPosition (0, transform.position); lineRenderer.SetPosition (1, laserHit.position); /*direction.transform.position = laserHit.position; Quaternion rotation = Quaternion.LookRotation(playerController.BurstDirecton.normalized); rotation.x = 0f; rotation.y = 0f; direction.transform.rotation = rotation;*/ //direction.transform.rotation = Quaternion.LookRotation(playerController.BurstDirecton.normalized) * _facing; }
IEnumerator nutGenerate() { yield return new WaitForSeconds (1f); rd = (Rigidbody2D)Instantiate (rd, transform.position, transform.rotation); rd.GetComponent<PolygonCollider2D> ().enabled = true; rd.isKinematic = false; done = true; //yield return new WaitForSeconds (1f); }
public override void OnStartLocalPlayer() { base.OnStartLocalPlayer(); body = GetComponent<Rigidbody2D>(); anim = GetComponent<Animator>(); netTransform = GetComponent<NetworkTransform>(); groundCheck = transform.Find("groundCheck"); heroAudSrc = body.GetComponent<AudioSource>(); }
/*--------------------------------------------------------------------------------------------------------------------- -- FUNCTION: Start -- -- DATE: March 9, 2016 -- -- REVISIONS: -- April 4, 2016 - Hank Lo: Balance changes, numbers fixed. -- April 5, 2016 - Hank Lo: Balance changes, numbers fixed more. -- -- DESIGNER: Hank Lo, Allen Tsang -- -- PROGRAMMER: Hank Lo, Allen Tsang -- -- INTERFACE: void Start(void) -- -- RETURNS: void -- -- NOTES: -- Function that's called when the script is first executed - it initializes all required values ---------------------------------------------------------------------------------------------------------------------*/ new void Start() { cooldowns = new float[2] { 0.75f, 4 }; healthBar = transform.GetChild(0).gameObject.GetComponent<HealthBar>(); _classStat = new PlayerBaseStat(playerID, healthBar); _classStat.MaxHp = 1200; _classStat.MoveSpeed = 7; Debug.Log ("[DEBUG2] Setting Ninja Speed: " + _classStat.MoveSpeed); _classStat.AtkPower = 80; _classStat.Defense = 60; base.Start(); sword = (Rigidbody2D)Resources.Load("Prefabs/NinjaSword", typeof(Rigidbody2D)); teleport = (GameObject)Resources.Load("Prefabs/NinjaTeleport", typeof(GameObject)); attack = (Rigidbody2D)Instantiate(sword, transform.position, transform.rotation); attack.GetComponent<BoxCollider2D>().enabled = false; attackMelee = attack.GetComponent<Melee>(); attackMelee.playerID = playerID; attackMelee.teamID = team; attackMelee.damage = ClassStat.AtkPower; attack.transform.parent = transform; var controller = Resources.Load("Controllers/ninjaboi") as RuntimeAnimatorController; gameObject.GetComponent<Animator>().runtimeAnimatorController = controller; //Player specific initialization if (playerID == GameData.MyPlayer.PlayerID) { //Starting item kit Inventory.instance.AddItem(1); } //ninja attack sound au_simple_attack = Resources.Load("Music/Weapons/ninjaboi_sword_primary") as AudioClip; au_special_attack = Resources.Load("Music/Weapons/ninjaboi_teleport") as AudioClip; Debug.Log ("[DEBUG] Setting Ninja Speed After Calling Base: " + _classStat.MoveSpeed); }
IEnumerator projectileInitiate() { //generate new projectile yield return new WaitForSeconds (1f); rd = (Rigidbody2D)Instantiate (rd, transform.position, transform.rotation); rd.GetComponent<PolygonCollider2D> ().enabled = false; rd.transform.parent = transform; rd.transform.localPosition = new Vector3 (1.4f, 0f, 0f); rd.transform.localScale = new Vector3 (1f, 1f, 1f); rd.isKinematic = true; }
//-------------------------------------------------------------------------------- #region Private Methods void GrabGoat(Rigidbody2D goat) { this.goat = goat; Debug.Log("Got the goat!"); goat.isKinematic = true; goat.MovePosition(transform.position); goat.GetComponent<Collider2D>().enabled = false; onGoatGrabbed.Invoke(); CharController player = GetComponentInParent<CharController>(); Goat.instance.lastCarry = player.playerNum; Alter.instance.CancelWin(); }
// Use this for initialization void Start() { startingJoystickSpot = transform.position; joystickAppearanceThreshhold = .8f; joystickMaxThumbDist = 1.3f; distToThrow = .1f; joystickFinger = -1; spearFinger = -2; moveForce = 10f; jaiScript = GameObject.Find ("Jai").GetComponent<Jai> (); controlStickSprite = GameObject.Find ("ControlStick").GetComponent<SpriteRenderer>(); jaiTransform = jaiScript.transform; balloonBasketBody = GameObject.Find ("BalloonBasket").GetComponent<Rigidbody2D>(); //correctionPixels = new Vector2 (Screen.width/2,-Screen.height/2); correctionPixels = new Vector2 (560,-960); //half of the screen width is 560 and yeah the heights weird [1280-640] //correctionPixelFactor = 5 / Screen.height; correctionPixelFactor = 5f / 320f; //5 game units divided by 320 pixels maxBalloonSpeed = balloonBasketBody.GetComponent<BalloonBasket>().maxBalloonSpeed; }
/// <summary> /// ReferencePoint 2D Constructor /// </summary> public ReferencePoint(Rigidbody2D body) { m_RigidBody2D = body; m_CircleCollider2D = body.GetComponent<CircleCollider2D>(); m_Transform = body.transform; m_IsDummy = false; }
public int PreformEvent(Rigidbody2D Body, int Speed, Animator Anim, int EventNumber, double X_Original, double Y_Original, int CaseStep) { //SavePosition(Body.gameObject.name); //<---- For saving an AI's location //Var for AI x and y float XMove = 0; float YMove = 0; if (X_Cutscene != 0 && Y_Cutscene != 0) { XMove = Find_X_Distance(Body, X_Cutscene); YMove = Find_Y_Distance(Body, Y_Cutscene); if (XMove == 0 && YMove == 0) { CaseStep++; GetLocation(Body, EventNumber, CaseStep); SaveInfo(Body, EventNumber, CaseStep); } } else { #region CutScene Finished Body.GetComponent<AI_Character>().Action = AI_Character.AI_Action.StationaryWithDir; Body.GetComponent<CircleCollider2D>().enabled = true; //---Check for teleport--- //Read in SaveGame.xml XmlDocument SaveGameDoc = new XmlDocument(); SaveGameDoc.LoadXml(Resources.Load("CutScenes/CutSceneEvents").ToString()); foreach (XmlNode node in SaveGameDoc.SelectNodes("CutSceneEvents/Scenes/Scene")) { if (Application.loadedLevelName == node.SelectSingleNode("SceneName").InnerText) { if (Body.name == "AI Josh") { #region AI Josh //Check for teleport if (node.SelectSingleNode("Part" + EventNumber + "/Teleport/JoshAI/X").InnerText != "" && node.SelectSingleNode("Part" + EventNumber + "/Teleport/JoshAI/Y").InnerText != "") { Body.GetComponent<Transform>().position = new Vector3(float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/JoshAI/X").InnerText), float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/JoshAI/Y").InnerText)); } //Read in SaveGame.xml XmlDocument xSaveGameDoc = new XmlDocument(); xSaveGameDoc.LoadXml(DoSaveGame.FetchSaveData()); //Find current scene then find ScenePart. foreach (XmlNode Xnode in xSaveGameDoc.SelectNodes("SaveData/SaveState/Scenes/Scene")) { if (Application.loadedLevelName == Xnode.SelectSingleNode("SceneName").InnerText) { Xnode.SelectSingleNode("AI/JoshAI/X").InnerText = Body.position.x.ToString(); Xnode.SelectSingleNode("AI/JoshAI/Y").InnerText = Body.position.y.ToString(); Xnode.SelectSingleNode("AI/JoshAI/Action").InnerText = ""; Xnode.SelectSingleNode("AI/JoshAI/EventType").InnerText = ""; Xnode.SelectSingleNode("AI/JoshAI/CaseStep").InnerText = ""; break; } } //Save XML DoSaveGame.UpdateSaveData(xSaveGameDoc); #endregion } else if (Body.name == "AI Matt") { #region AI Matt //Check for teleport if (node.SelectSingleNode("Part" + EventNumber + "/Teleport/MattAI/X").InnerText != "" && node.SelectSingleNode("Part" + EventNumber + "/Teleport/MattAI/Y").InnerText != "") { Body.GetComponent<Transform>().position = new Vector3(float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/MattAI/X").InnerText), float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/MattAI/Y").InnerText)); } //Read in SaveGame.xml XmlDocument xSaveGameDoc = new XmlDocument(); xSaveGameDoc.LoadXml(DoSaveGame.FetchSaveData()); //Find current scene then find ScenePart. foreach (XmlNode Xnode in xSaveGameDoc.SelectNodes("SaveData/SaveState/Scenes/Scene")) { if (Application.loadedLevelName == Xnode.SelectSingleNode("SceneName").InnerText) { Xnode.SelectSingleNode("AI/MattAI/X").InnerText = Body.position.x.ToString(); Xnode.SelectSingleNode("AI/MattAI/Y").InnerText = Body.position.y.ToString(); Xnode.SelectSingleNode("AI/MattAI/Action").InnerText = ""; Xnode.SelectSingleNode("AI/MattAI/EventType").InnerText = ""; Xnode.SelectSingleNode("AI/MattAI/CaseStep").InnerText = ""; } } //Save XML DoSaveGame.UpdateSaveData(xSaveGameDoc); #endregion } else if (Body.name == "AI Kate") { #region AI Kate //Check for teleport if (node.SelectSingleNode("Part" + EventNumber + "/Teleport/KateAI/X").InnerText != "" && node.SelectSingleNode("Part" + EventNumber + "/Teleport/KateAI/Y").InnerText != "") { Body.GetComponent<Transform>().position = new Vector3(float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/KateAI/X").InnerText), float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/KateAI/Y").InnerText)); } //Read in SaveGame.xml XmlDocument xSaveGameDoc = new XmlDocument(); xSaveGameDoc.LoadXml(DoSaveGame.FetchSaveData()); //Find current scene then find ScenePart. foreach (XmlNode Xnode in xSaveGameDoc.SelectNodes("SaveData/SaveState/Scenes/Scene")) { if (Application.loadedLevelName == Xnode.SelectSingleNode("SceneName").InnerText) { Xnode.SelectSingleNode("AI/KateAI/X").InnerText = Body.position.x.ToString(); Xnode.SelectSingleNode("AI/KateAI/Y").InnerText = Body.position.y.ToString(); Xnode.SelectSingleNode("AI/KateAI/Action").InnerText = ""; Xnode.SelectSingleNode("AI/KateAI/EventType").InnerText = ""; Xnode.SelectSingleNode("AI/KateAI/CaseStep").InnerText = ""; } } //Save XML DoSaveGame.UpdateSaveData(xSaveGameDoc); #endregion } else if (Body.name == "AI April") { #region AI April //Check for teleport if (node.SelectSingleNode("Part" + EventNumber + "/Teleport/AprilAI/X").InnerText != "" && node.SelectSingleNode("Part" + EventNumber + "/Teleport/AprilAI/Y").InnerText != "") { Body.GetComponent<Transform>().position = new Vector3(float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/AprilAI/X").InnerText), float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/AprilAI/Y").InnerText)); } //Read in SaveGame.xml XmlDocument xSaveGameDoc = new XmlDocument(); xSaveGameDoc.LoadXml(DoSaveGame.FetchSaveData()); //Find current scene then find ScenePart. foreach (XmlNode Xnode in xSaveGameDoc.SelectNodes("SaveData/SaveState/Scenes/Scene")) { if (Application.loadedLevelName == Xnode.SelectSingleNode("SceneName").InnerText) { Xnode.SelectSingleNode("AI/AprilAI/X").InnerText = Body.position.x.ToString(); Xnode.SelectSingleNode("AI/AprilAI/Y").InnerText = Body.position.y.ToString(); Xnode.SelectSingleNode("AI/AprilAI/Action").InnerText = ""; Xnode.SelectSingleNode("AI/AprilAI/EventType").InnerText = ""; Xnode.SelectSingleNode("AI/AprilAI/CaseStep").InnerText = ""; } } //Save XML DoSaveGame.UpdateSaveData(xSaveGameDoc); #endregion } else if (Body.name == "AI Ethan") { #region AI Ethan //Check for teleport if (node.SelectSingleNode("Part" + EventNumber + "/Teleport/EthanAI/X").InnerText != "" && node.SelectSingleNode("Part" + EventNumber + "/Teleport/EthanAI/Y").InnerText != "") { Body.GetComponent<Transform>().position = new Vector3(float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/EthanAI/X").InnerText), float.Parse(node.SelectSingleNode("Part" + EventNumber + "/Teleport/EthanAI/Y").InnerText)); } //Read in SaveGame.xml XmlDocument xSaveGameDoc = new XmlDocument(); xSaveGameDoc.LoadXml(DoSaveGame.FetchSaveData()); //Find current scene then find ScenePart. foreach (XmlNode Xnode in xSaveGameDoc.SelectNodes("SaveData/SaveState/Scenes/Scene")) { if (Application.loadedLevelName == Xnode.SelectSingleNode("SceneName").InnerText) { Xnode.SelectSingleNode("AI/EthanAI/X").InnerText = Body.position.x.ToString(); Xnode.SelectSingleNode("AI/EthanAI/Y").InnerText = Body.position.y.ToString(); Xnode.SelectSingleNode("AI/EthanAI/Action").InnerText = ""; Xnode.SelectSingleNode("AI/EthanAI/EventType").InnerText = ""; Xnode.SelectSingleNode("AI/EthanAI/CaseStep").InnerText = ""; } } //Save XML DoSaveGame.UpdateSaveData(xSaveGameDoc); #endregion } } } #endregion } //Execute Movement PlayerMovement.PlayerMove(Body, Speed, Anim, XMove, YMove); return CaseStep; }
public static void Store(Rigidbody2D rigidbody) { Rigidbody2DSettingsHolder settingsHolder = rigidbody.GetComponent<Rigidbody2DSettingsHolder>(); if (settingsHolder == null) { settingsHolder = rigidbody.gameObject.AddComponent<Rigidbody2DSettingsHolder>(); } settingsHolder.Set(rigidbody); }
public static void Restore(Rigidbody2D rigidbody) { Rigidbody2DSettingsHolder settingsHolder = rigidbody.GetComponent<Rigidbody2DSettingsHolder>(); if (settingsHolder != null) { rigidbody.isKinematic = settingsHolder.settings.isKinematic; rigidbody.velocity = settingsHolder.settings.velocity; rigidbody.angularVelocity = settingsHolder.settings.angularVelocity; rigidbody.mass = settingsHolder.settings.mass; rigidbody.drag = settingsHolder.settings.linearDrag; rigidbody.angularDrag = settingsHolder.settings.angularDrag; rigidbody.gravityScale = settingsHolder.settings.gravityScale; rigidbody.interpolation = settingsHolder.settings.interpolate; rigidbody.collisionDetectionMode = settingsHolder.settings.collisionDetection; rigidbody.sleepMode = settingsHolder.settings.sleepMode; rigidbody.constraints = settingsHolder.settings.constraints; Object.Destroy(settingsHolder); } else Debug.LogError("Cannot restore rigidbody (" + rigidbody.name + ")! No settings holder found"); }
private void releaseRigidBody(Rigidbody2D rb) { rb.fixedAngle = false; rb.gameObject.AddComponent<PartDestroyer>(); HingeJoint2D hingeJoint = rb.GetComponent<HingeJoint2D>(); if(hingeJoint) GameObject.Destroy(hingeJoint); rb.transform.parent = null; }
void GenerateBeltEnd( Vector3 center, Vector3 direction, bool leftEnd, ref Rigidbody2D startLink, ref Rigidbody2D endLink) { ConveyorBehavior conveyorBehavior = target as ConveyorBehavior; DistanceJoint2D joint; float axleRadius = conveyorBehavior.axleRadius; float arcLength = axleRadius * Mathf.PI; int numLinks = Mathf.CeilToInt(arcLength * conveyorBehavior.linksPerUnit); Vector3 up = Quaternion.FromToRotation(Vector3.right, Vector3.up) * direction; up.Normalize(); for (int i = 1; i < numLinks - 1; ++i) { // build CCW... float arcFraction = -Mathf.Lerp(0, Mathf.PI, (float) i / (float) (numLinks - 1)); Quaternion rotation = Quaternion.AngleAxis(Mathf.Rad2Deg * arcFraction, Vector3.forward); Vector3 position = center + rotation * (up * axleRadius); if (leftEnd) { rotation *= Quaternion.AngleAxis(180, Vector3.forward); } GameObject link = Instantiate(conveyorBehavior.beltLinkPrefab, position, rotation) as GameObject; link.transform.parent = conveyorBehavior.beltContainer.transform; joint = endLink.GetComponent<DistanceJoint2D>(); joint.connectedBody = link.rigidbody2D; joint.distance = conveyorBehavior.linkDistance; endLink = link.rigidbody2D; } }
// Throw direction should be a normalized vector. Initial anchor is probably the player's body public void Throw_Rope(Vector3 start_position, Vector2 throw_direction, float throw_force, Rigidbody2D initial_anchor, GameObject thrower) { // Spawn a number of segments for (int i = 0; i < number_of_segments; i++) { GameObject segment = ((GameObject)Instantiate(emptyPrefab, new Vector3(start_position.x, start_position.y, 0), Quaternion.identity)); joints.Add(segment); segment.transform.parent = transform; } // Connect them with hingejoints for (int j = 0; j < joints.Count - 1; j++) { joints[j].transform.parent = this.transform; joints[j].GetComponent<HingeJoint2D>().connectedBody = joints[j + 1].GetComponent<Rigidbody2D>(); } // Set their neighbours for (int x = 0; x < number_of_segments; x++) { int above_int = Mathf.Clamp(x - 1, 0, number_of_segments - 1); int below_int = Mathf.Clamp(x + 1, 0, number_of_segments - 1); joints[x].GetComponent<Link>().above = joints[above_int]; joints[x].GetComponent<Link>().below = joints[below_int]; joints[x].GetComponent<Link>().top_most = joints[0]; joints[x].GetComponent<Link>().bottom_most = joints[number_of_segments - 1]; joints[x].GetComponent<Link>().position_from_top_in_rope = x; joints[x].GetComponent<Link>().position_from_bottom_in_rope = number_of_segments - x; joints[x].GetComponent<Link>().all_segments = joints; joints[x].GetComponent<Link>().rope = this; } // Disable the hingejoint on the last rope semgnet if (initial_anchor == null) { joints[joints.Count - 1].GetComponent<HingeJoint2D>().enabled = false; joints[joints.Count - 1].GetComponent<HingeJoint2D>().connectedAnchor = Vector2.zero; } else { // Make the end tied to the player's waist /* joints[joints.Count - 1].GetComponent<HingeJoint2D>().connectedBody = initial_anchor; joints[joints.Count - 1].GetComponent<HingeJoint2D>().connectedAnchor = Vector2.zero; initial_anchor.GetComponent<PlatformerCharacter2D>().connected_joint = joints[joints.Count - 1].GetComponent<HingeJoint2D>(); */ joints[joints.Count - 1].GetComponent<HingeJoint2D>().enabled = false; joints[joints.Count - 1].GetComponent<HingeJoint2D>().connectedAnchor = Vector2.zero; // Activate the joint on the player object, and attach it to the end rope segment PlatformerCharacter2D player = initial_anchor.GetComponent<PlatformerCharacter2D>(); player.connected_joint.enabled = true; player.connected_joint.connectedBody = joints[joints.Count - 1].GetComponent<Rigidbody2D>(); //player.rope_follower.SetActive(true); //player.rope_follower.GetComponent<FollowObject>().object_to_follow = joints[joints.Count - 1].transform; //player.connected_joint.connectedBody = player.rope_follower.GetComponent<Rigidbody2D>(); } // Make the start attachable to the terrain, like a grappling hook AttachToTerrain terrain = joints[0].AddComponent<AttachToTerrain>(); terrain.thrower = thrower; terrain.attach_to_thrower_if_in_range = true; terrain.attach_range = joints[0].GetComponent<CircleCollider2D>().radius * 2 * joints.Count; // First segment always has a spring with which to attack the player to SpringJoint2D spring = joints[0].AddComponent<SpringJoint2D>(); spring.dampingRatio = 0; spring.frequency = 0; spring.enabled = false; // Name the segments joints[0].name = "FirstPiece"; joints[joints.Count - 1].name = "EndPiece"; // Create attach points at the top and bottom of the rope GameObject top = (GameObject) Instantiate(Resources.Load("RopeAttachPoint"), joints[0].transform.position, Quaternion.identity); top.transform.parent = joints[0].transform; GameObject bottom = (GameObject)Instantiate(Resources.Load("RopeAttachPoint"), joints[joints.Count - 1].transform.position, Quaternion.identity); bottom.transform.parent = joints[joints.Count - 1].transform; // Make the first segment able to attach to other ropes top.AddComponent<RopeCombiner>(); // Add a force to the first rope segment for (int z = 0; z < joints.Count; z++) { Vector2 force_to_add = throw_direction * throw_force * (1f - (float) ((float)(z) / (float)joints.Count)); joints[z].GetComponent<Rigidbody2D>().AddForce(force_to_add); } /* joints[0].GetComponent<Rigidbody2D>().AddForce(throw_direction * throw_force); joints[1].GetComponent<Rigidbody2D>().AddForce(throw_direction * (throw_force * 0.5f)); joints[2].GetComponent<Rigidbody2D>().AddForce(throw_direction * (throw_force * 0.4f)); joints[3].GetComponent<Rigidbody2D>().AddForce(throw_direction * (throw_force * 0.3f)); joints[4].GetComponent<Rigidbody2D>().AddForce(throw_direction * (throw_force * 0.2f)); joints[5].GetComponent<Rigidbody2D>().AddForce(throw_direction * (throw_force * 0.1f)); joints[6].GetComponent<Rigidbody2D>().AddForce(throw_direction * (throw_force * 0.1f)); joints[7].GetComponent<Rigidbody2D>().AddForce(throw_direction * (throw_force * 0.1f)); joints[8].GetComponent<Rigidbody2D>().AddForce(throw_direction * (throw_force * 0.1f)); joints[9].GetComponent<Rigidbody2D>().AddForce(throw_direction * (throw_force * 0.1f));*/ // Add rope to dictionary of ropes ObjectManager.object_manager.AddRope(this.gameObject); }
private void addForce(Rigidbody2D rigBody) { RaycastHit2D rh = Physics2D.Linecast (transform.position, rigBody.transform.position); float force = (addForce (rh.distance)); float angle = getAngle(transform.position,rigBody.transform.position); Vector2 forceVector = new Vector2(-Mathf.Cos(angle),-Mathf.Sin(angle)); forceVector *= force; Debug.DrawRay (rigBody.transform.position,forceVector.normalized,Color.cyan,5f,false); rigBody.AddForceAtPosition (forceVector,rh.point); if(rigBody.GetComponent<Snappable>()){ if (rigBody.GetComponent<Snappable>().snapOnExplode) { rigBody.GetComponent<Snappable>().destroy(); } } if (rigBody.GetComponent<explosionForce>()) { explosionForce e = rigBody.GetComponent<explosionForce>(); if (e.explodeWithExplosion) { StartCoroutine(exp(e)); } } }
// Use this for initialization void Start() { m_HeldBlock = ObjectPoolManager.PullObject(k_BlockTag).GetComponent<Rigidbody2D>(); m_HeldBlock.GetComponent<Collider2D>().enabled = false; SpriteRenderer sprite = m_HeldBlock.GetComponent<SpriteRenderer>(); m_HeightOfBlockInPixels = sprite.sprite.texture.height; m_WidthOfBlockInPixels = sprite.sprite.texture.width; Camera.main.orthographicSize = Camera.main.pixelHeight / sprite.sprite.pixelsPerUnit / 2; StartCoroutine(scrollUpForGameStart()); m_HorizontalDropPosition = m_RightBorderHorizontalPosition = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width - m_WidthOfBlockInPixels / 2, Screen.height / 2, transform.position.z)).x; m_LeftBorderHorizontalPosition = Camera.main.ScreenToWorldPoint(new Vector3(0 + m_WidthOfBlockInPixels / 2, Screen.height / 2, transform.position.z)).x; m_PlacerMover = StartCoroutine(movePlacerPosition()); }
// Use this for initialization void Start() { m_ScoreText = GameObject.FindGameObjectWithTag(k_ScoreTextTag).GetComponent<Text>(); m_HeldBlock = ObjectPoolManager.PullObject(k_BlockTag).GetComponent<Rigidbody2D>(); m_HeldBlock.GetComponent<Collider2D>().enabled = false; m_HeldBlock.gravityScale = 0; SpriteRenderer sprite = m_HeldBlock.GetComponent<SpriteRenderer>(); //Camera.main.orthographicSize = Screen.height / 2 / sprite.sprite.pixelsPerUnit; m_HeightOfBlockInPixels = sprite.sprite.pixelsPerUnit; m_WidthOfBlockInPixels = sprite.sprite.texture.width / (sprite.sprite.texture.height / m_HeightOfBlockInPixels); StartCoroutine(scrollUpForGameStart()); m_HorizontalDropPosition = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width - m_WidthOfBlockInPixels, Screen.height / 2, transform.position.z)).x; m_PlacerMover = StartCoroutine(movePlacerPosition()); }
// FixedUpdate is used for physics related tasks to maintain accuracy. void FixedUpdate() { // Debug movement controls. #region DebugMovement //if (Input.GetKey(KeyCode.W)) //{ // this.transform.position += new Vector3(0, 0.1f,0); //} //if (Input.GetKey(KeyCode.D)) //{ // this.transform.position += new Vector3(0.1f, 0, 0); //} //if (Input.GetKey(KeyCode.S)) //{ // this.transform.position += new Vector3(0, -0.1f, 0); //} //if (Input.GetKey(KeyCode.A)) //{ // this.transform.position += new Vector3(-0.1f, 0, 0); //} #endregion // Code handling the firing of web. #region FiringWeb switch (fireState) { // Firing Case case 1: // Once every 20 frames... if (pointCounter == 0) { // Create a new node to interact with currentNode = Instantiate(webPoint, this.transform.position, new Quaternion(0, 0, 0, 0)) as Rigidbody2D; currentNode.tag = "Node"; numNodes++; // Tell it the next node in the chain. currentNode.GetComponent<WebNode>().nextNode = prevNode.GetComponent<WebNode>(); // If there is a next node in the chain, inform that next node about the new one. if (prevNode != null) { prevNode.GetComponent<WebNode>().prevNode = currentNode.GetComponent<WebNode>(); } // Tell the next node in the chain to have a spring between it and the new node. prevNode.GetComponent<SpringJoint2D>().connectedBody = currentNode.rigidbody2D; // Fire the previous node prevNode.GetComponent<WebNode>().Activate(); // Get the worldspace position of the mouse Vector3 mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 fireDirection = new Vector2(mousePoint.x - this.transform.position.x, mousePoint.y - this.transform.position.y); // Fire it with arbitrary force. fireDirection.Normalize(); fireDirection.Scale(new Vector2(2200,2200)); prevNode.rigidbody2D.AddForce(fireDirection ); // Move the nodes along the chain prevNode = currentNode; currentNode = null; } // Increase the timer until the next fire. pointCounter++; // Arbitrary reset point if (pointCounter >= 10) { pointCounter = 0; } break; // Holding Case case 2: // If the next node is too close to the previous node while being held, then suck it in. if (prevNode.GetComponent<WebNode>().nextNode != null && (prevNode.GetComponent<WebNode>().nextNode.rigidbody2D.position - prevNode.position).magnitude < 0.9f) { // Create a temporary node to allow access to the next rigidbody. Rigidbody2D temp = prevNode.GetComponent<WebNode>().nextNode.rigidbody2D; // Remove references to the prevNode temp.GetComponent<SpringJoint2D>().connectedBody = null; temp.GetComponent<SpringJoint2D>().enabled = false; // Destroy it, and set PrevNode = to the temporary node. Destroy(prevNode.gameObject); numNodes--; prevNode = temp; prevNode.rigidbody2D.isKinematic = true; prevNode.rigidbody2D.GetComponent<WebNode>().prevNode = null; } break; // Reset Case case 3: // Reset all parts into their base sections. pointCounter = 0; currentNode = null; prevNode.GetComponent<WebNode>().firstNode = true; prevNode.GetComponent<WebNode>().numNodes = numNodes; prevNode = null; fireState = 0; numNodes = 0; break; } #endregion // Player node attacher management. if (prevNode == null || prevNode.GetComponent<WebNode>().nextNode == null) { this.GetComponentInParent<SpringJoint2D>().enabled = false; } else { this.GetComponentInParent<SpringJoint2D>().connectedBody = prevNode.GetComponent<WebNode>().nextNode.rigidbody2D; this.GetComponentInParent<SpringJoint2D>().enabled = true; } }
void GenerateStraightBelt( Vector3 start, Vector3 end, ref Rigidbody2D startLink, ref Rigidbody2D endLink) { ConveyorBehavior conveyorBehavior = target as ConveyorBehavior; Vector3 startToEnd = end - start; Vector3 up = Quaternion.FromToRotation(Vector3.right, Vector3.up) * startToEnd; float length = Vector3.Distance(start, end); int numLinks = Mathf.FloorToInt(length * conveyorBehavior.linksPerUnit); GameObject link; DistanceJoint2D joint; link = Instantiate(conveyorBehavior.beltLinkPrefab, start, Quaternion.FromToRotation(Vector3.up, up)) as GameObject; link.transform.eulerAngles = new Vector3(0, 0, link.transform.eulerAngles.z); link.transform.parent = conveyorBehavior.beltContainer.transform; if (startLink == null && endLink == null) { startLink = link.rigidbody2D; endLink = link.rigidbody2D; } else { joint = endLink.GetComponent<DistanceJoint2D>(); joint.connectedBody = link.rigidbody2D; joint.distance = conveyorBehavior.linkDistance; endLink = link.rigidbody2D; } for (int i = 1; i < numLinks - 1; ++i) { Vector3 position = Vector3.Lerp(start, end, (float) i / (float) (numLinks - 1)); link = Instantiate(conveyorBehavior.beltLinkPrefab, position, Quaternion.FromToRotation(Vector3.up, up)) as GameObject; link.transform.eulerAngles = new Vector3(0, 0, link.transform.eulerAngles.z); link.transform.parent = conveyorBehavior.beltContainer.transform; joint = endLink.GetComponent<DistanceJoint2D>(); joint.connectedBody = link.rigidbody2D; joint.distance = conveyorBehavior.linkDistance; endLink = link.rigidbody2D; } link = Instantiate(conveyorBehavior.beltLinkPrefab, end, Quaternion.FromToRotation(Vector3.up, up)) as GameObject; link.transform.eulerAngles = new Vector3(0, 0, link.transform.eulerAngles.z); link.transform.parent = conveyorBehavior.beltContainer.transform; joint = endLink.GetComponent<DistanceJoint2D>(); joint.connectedBody = link.rigidbody2D; joint.distance = conveyorBehavior.linkDistance; endLink = link.rigidbody2D; }
private void noclipRigidBody(Rigidbody2D rb) { BoxCollider2D boxCollider2D = rb.GetComponent<BoxCollider2D>(); if(boxCollider2D) GameObject.Destroy(boxCollider2D); }