/* From the Positions of the Player2 we calculate the amount of movement to move every point to keep them togeher */ private void DistMaxP2() { bool stop = false; for (int PointIndex = NumPoints - 1; PointIndex > 1 && !stop; PointIndex--) { Rope_Point ParticleA = Points[PointIndex]; Rope_Point ParticleB = Points[PointIndex - 1]; Vector3 Delta = (ParticleA.transform.position + ParticleA.new_pos_p2 + ParticleA.new_pos) - (ParticleB.transform.position + ParticleB.new_pos); float CurrentDistance = Delta.magnitude; if (CurrentDistance > MaxLenght_xPoint) { if (!ParticleB.enemie_coll) { Vector3 P_B = (CurrentDistance - MaxLenght_xPoint) * Delta.normalized; ParticleB.new_pos_p2 = P_B; } else { Vector3 P_B = (CurrentDistance - MaxLenght_xPoint) * (Delta.normalized * 0.2f); ParticleB.new_pos_p2 = P_B; } } else { stop = true; } } }
private void Chain_PhysicsUpdate(Vector3 P1_mov, Vector3 P2_mov) { // IF with the MovePositions some points are + far from MaxLenght (for collisions,etc..) we use transform position instead to correct their position CentredForce(NumPoints / 2); Points[0].new_pos_p1 = P1_mov; Points[NumPoints - 1].new_pos_p2 = P2_mov; for (int curr_inte = 0; curr_inte <= Iterations; curr_inte++) { //We calcule the new position of the point depending of the movement of the extrems points DistMaxP1(); DistMaxP2(); DistMaxCalcul(); } //Moving the points for (int PointIndex = 0; PointIndex < NumPoints; PointIndex++) { Rope_Point ParticleA = Points[PointIndex]; ParticleA.GetComponent <Rigidbody2D>().MovePosition((Vector2)ParticleA.transform.position + (Vector2)ParticleA.new_pos); ParticleA.new_pos_p1 = Vector3.zero; ParticleA.new_pos_p2 = Vector3.zero; ParticleA.new_pos = Vector3.zero; } }
// Use this for initialization void Start() { //The layers to ignore Physics2D.IgnoreLayerCollision(19, 9); Physics2D.IgnoreLayerCollision(20, 9); Physics2D.IgnoreLayerCollision(9, 9); Physics2D.IgnoreLayerCollision(9, 15); Points.Clear(); mov_P1 = Vector3.zero; mov_P2 = Vector3.zero; // "Distance" between the 2 start points Vector3 Delta = CableEnd.position - CableStart.position; //Instantiate the points of the chain for (int ParticleIndex = 0; ParticleIndex < NumPoints; ParticleIndex++) { Rope_Point particle = Instantiate(PrefabPoint, Vector3.zero, Quaternion.identity); float Alpha = (float)ParticleIndex / (float)(NumPoints - 1); Vector3 InitializePosition = CableStart.transform.position + (Alpha * Delta); //Ini variables of the Particle particle.transform.position = InitializePosition; particle.transform.parent = this.transform; particle.transform.tag = "rope"; particle.name = "Point_" + ParticleIndex.ToString(); particle.gameObject.layer = 9; Points.Add(particle); if (ParticleIndex == 0) { particle.gameObject.layer = 19; } else if (ParticleIndex == (NumPoints - 1)) { particle.gameObject.layer = 20; } if (ParticleIndex % 2 == 0) { particle.GetComponent <SpriteRenderer>().sprite = chainA; } else { particle.GetComponent <SpriteRenderer>().sprite = chainB; } } }
private void DistMaxCalcul() { for (int PointIndex = 0; PointIndex < NumPoints; PointIndex++) { Rope_Point ParticleA = Points[PointIndex]; ParticleA.new_pos += ParticleA.new_pos_p1 + ParticleA.new_pos_p2; ParticleA.new_pos_p1 = Vector3.zero; ParticleA.new_pos_p2 = Vector3.zero; } }
public void change_NumPoints() { int New_NumPoints = Mathf.RoundToInt(slider_rope_l.value); if (New_NumPoints != rope_system.NumPoints) { int dif_NumPoint = New_NumPoints - rope_system.NumPoints; if (dif_NumPoint > 0) { for (int x = 0; x < dif_NumPoint; x++) { Rope_Point particle = Instantiate(rope_system.PrefabPoint, Vector3.zero, Quaternion.identity); Vector3 InitializePosition = rope_system.get_points()[rope_system.NumPoints - 1].transform.position; particle.transform.position = InitializePosition; particle.transform.parent = rope_system.transform; particle.transform.tag = "rope"; particle.name = "Point_" + (rope_system.NumPoints + x).ToString(); particle.gameObject.layer = 9; if ((rope_system.NumPoints + x) % 2 == 0) { particle.GetComponent <SpriteRenderer>().enabled = false; } rope_system.get_points().Add(particle); } rope_system.NumPoints = rope_system.NumPoints + dif_NumPoint; } else { for (int x = 0; x < -dif_NumPoint; x++) { Rope_Point rp = rope_system.get_points()[rope_system.NumPoints - 1 - x]; rope_system.get_points().RemoveAt(rope_system.NumPoints - 1 - x); Destroy(rp.gameObject); } rope_system.NumPoints = rope_system.NumPoints + dif_NumPoint; } } }
//We apply a force from the center of the chain ("to inside") to keep the points together //If the chain is colliding an enemie the center is changed to this one private void CentredForce(int Index_coll) { int new_indew = Index_coll; //We search if a point is colliding with an enemie, if yes, we change de index coll to use after List <Rope_Point> lisr_p = new List <Rope_Point>(); for (int PointIndex = 0; PointIndex < NumPoints - 1; PointIndex++) { Rope_Point ParticleA = Points[PointIndex]; if (ParticleA.coll_state || ParticleA.enemie_coll) { lisr_p.Add(ParticleA); } } if (lisr_p.Count != 0) { Rope_Point p = lisr_p[lisr_p.Count / 2]; new_indew = Points.IndexOf(p); } // --> To the Right for (int PointIndex = new_indew; PointIndex < NumPoints - 1; PointIndex++) { Rope_Point ParticleA = Points[PointIndex]; Rope_Point ParticleB = Points[PointIndex + 1]; Vector3 Delta = ParticleA.transform.position - ParticleB.transform.position; float CurrentDistance = Delta.magnitude; if (CurrentDistance > MaxLenght_xPoint) { if (!ParticleB.enemie_coll) { ParticleB.transform.position += Delta.normalized * ((CurrentDistance - MaxLenght_xPoint)); } else { ParticleB.transform.position += (Delta.normalized * 0.2f) * ((CurrentDistance - MaxLenght_xPoint)); } } } // <-- To the left for (int PointIndex = new_indew; PointIndex > 0; PointIndex--) { Rope_Point ParticleA = Points[PointIndex]; Rope_Point ParticleB = Points[PointIndex - 1]; Vector3 Delta = ParticleA.transform.position - ParticleB.transform.position; float CurrentDistance = Delta.magnitude; if (CurrentDistance > MaxLenght_xPoint) { if (!ParticleB.enemie_coll) { ParticleB.transform.position += Delta.normalized * ((CurrentDistance - MaxLenght_xPoint)); } else { ParticleB.transform.position += (Delta.normalized * 0.2f) * ((CurrentDistance - MaxLenght_xPoint)); } } } }
// Use this for initialization void Start() { state_surround = false; Points.Clear(); mov_P1 = Vector3.zero; mov_P2 = Vector3.zero; _lineRenderer = transform.GetComponent <LineRenderer>(); if (_lineRenderer == null) { _lineRenderer = gameObject.AddComponent <LineRenderer>(); } _lineRenderer.positionCount = NumPoints + 2; _lineRenderer.startWidth = 0.15f; _lineRenderer.endWidth = 0.15f; // "Distance" between the 2 start points Vector3 Delta = CableEnd.position - CableStart.position; for (int ParticleIndex = 0; ParticleIndex < NumPoints; ParticleIndex++) { Rope_Point particle = Instantiate(PrefabPoint, Vector3.zero, Quaternion.identity); float Alpha = (float)ParticleIndex / (float)(NumPoints - 1); Vector3 InitializePosition = CableStart.transform.position + (Alpha * Delta); //Ini variables of the Particle particle.transform.position = InitializePosition; particle.transform.parent = this.transform; particle.transform.tag = "rope"; particle.name = "Point_" + ParticleIndex.ToString(); particle.gameObject.layer = 9; //TODO: Move Ignore Layer collision outside of loop Physics2D.IgnoreLayerCollision(8, 9);////////////////// Physics2D.IgnoreLayerCollision(9, 9); Points.Add(particle); if (ParticleIndex % 2 == 0) { particle.GetComponent <SpriteRenderer>().sprite = chainA; particle.GetComponent <SpriteRenderer>().sortingOrder = 2; } else { particle.GetComponent <SpriteRenderer>().sprite = chainB; } } //ELST for (int ParticleIndex = 0; ParticleIndex < NumPoints; ParticleIndex++) { if (ParticleIndex == 0) { SpringJoint2D spring = Points[ParticleIndex].gameObject.AddComponent <SpringJoint2D>(); spring.autoConfigureDistance = false; spring.distance = 0.3f; spring.frequency = 8; spring.connectedBody = player_One.GetComponent <Rigidbody2D>(); } else if (ParticleIndex < NumPoints / 2) { SpringJoint2D spring = Points[ParticleIndex].gameObject.AddComponent <SpringJoint2D>(); spring.autoConfigureDistance = false; spring.distance = 0.3f; spring.frequency = 8; spring.connectedBody = Points[ParticleIndex - 1].GetComponent <Rigidbody2D>(); } else if (ParticleIndex == NumPoints / 2) { SpringJoint2D spring = Points[ParticleIndex].gameObject.AddComponent <SpringJoint2D>(); spring.autoConfigureDistance = false; spring.distance = 0.3f; spring.frequency = 8; spring.connectedBody = Points[ParticleIndex - 1].GetComponent <Rigidbody2D>(); SpringJoint2D spring2 = Points[ParticleIndex].gameObject.AddComponent <SpringJoint2D>(); spring2.autoConfigureDistance = false; spring2.distance = 0.3f; spring2.frequency = 8; spring2.connectedBody = Points[ParticleIndex + 1].GetComponent <Rigidbody2D>(); } else if (ParticleIndex == NumPoints - 1) { SpringJoint2D spring = Points[ParticleIndex].gameObject.AddComponent <SpringJoint2D>(); spring.autoConfigureDistance = false; spring.distance = 0.3f; spring.frequency = 8; spring.connectedBody = player_Two.GetComponent <Rigidbody2D>(); } else if (ParticleIndex > NumPoints / 2) { SpringJoint2D spring = Points[ParticleIndex].gameObject.AddComponent <SpringJoint2D>(); spring.autoConfigureDistance = false; spring.distance = 0.3f; spring.frequency = 8; spring.connectedBody = Points[ParticleIndex + 1].GetComponent <Rigidbody2D>(); } } }