Hello, Im making a 2D soccer game and when one of the players score a goal, the players go back to the starting position and one random ball is instantiated in the center of the field, just like it happens in soccer.
The problem is, when the players teleport, they keep the momentum they had before teleporting, and I tried adding ( pBody.velocity = Vector2.zero; ) along the other teleporting instructions, and AFAIK it should do exactly that, stop all movement from the rigidbody, but thats not happening, Script:
public class MatchManager : MonoBehaviour {
public GameObject[] balls;
public GameObject player1;
public GameObject player2;
GameObject ball;
Rigidbody2D pBody1;
Rigidbody2D pBody2;
Vector2 ballPos;
Vector2 player1Pos;
Vector2 player2Pos;
// Use this for initialization
void Start () {
ballPos = new Vector2(0, 6);
player1Pos = new Vector2(4.2f, 2.35f);
player2Pos = new Vector2(-4.2f, 2.35f);
StartRound();
pBody1 = player1.GetComponent<Rigidbody2D>();
pBody2 = player2.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
ball = GameObject.FindGameObjectWithTag("Ball");
}
public void StartRound () {
if (ball !=null) {
Destroy(ball);
}
Instantiate(balls[Random.Range(0, balls.Length)], ballPos, transform.rotation);
player1.transform.position = player1Pos;
player1.transform.rotation = Quaternion.identity;
pBody1.velocity = Vector2.zero;
pBody1.angularVelocity = 0;
player2.transform.position = player2Pos;
player2.transform.rotation = Quaternion.identity;
pBody2.velocity = Vector2.zero;
pBody2.angularVelocity = 0;
}
}
Also I already tried to do the same with a single private rigidbody, same result.
Im pretty new to unity and I might be missing something here,
Thanks in advance!