rigidbody velocity = vector2 zero not working ?
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!
The idea is fine. Assigning all 0's to velocity stops you. Sometimes you also need to assign 0 to the angular velocity, to stop it from spinning. If you're still worried, try a small test -- make a ball that falls, with pressing a key zero-ing out velocity.
Answer by UnityCoach · Oct 09, 2018 at 02:05 AM
You need to assign pBody1 and pBody2 before calling them.
Simply move your StartRound () method call after their assignment.
Thank you! I totally forgot about that, but I tried it and unless I messed up the assignment, It still doesnt work for unknown reason, heres what I changed:
void Start () {
ballPos = new Vector2(0, 6);
player1Pos = new Vector2(4.2f, 2.35f);
player2Pos = new Vector2(-4.2f, 2.35f);
pBody1 = player1.GetComponent<Rigidbody2D>();
pBody2 = player2.GetComponent<Rigidbody2D>();
StartRound();
}
Still the same issue :(
Here, I did a bit of groo$$anonymous$$g in there.
public class $$anonymous$$atch$$anonymous$$anager : $$anonymous$$onoBehaviour
{
[SerializeField] GameObject[] balls;
[SerializeField] Rigidbody2D player1;
[SerializeField] Rigidbody2D player2;
GameObject ball;
[SerializeField] Transform ballPos; // assign this in the Editor, much easier to control than with absolute positions in the code
Vector2 player1Pos; // this will store original player position
Vector2 player2Pos;
void Start ()
{
player1Pos = player1.transform.position; // fetching position from Rigidbody2D's associated transform
player2Pos = player2.transform.position;
StartRound();
}
public void StartRound ()
{
if (ball !=null)
Destroy(ball);
ball = (GameObject) Instantiate(balls[Random.Range(0, balls.Length)], ballPos, transform.rotation); // assigning the result, no need to use Find later
player1.position = player1Pos; // changing rigidbody's position ins$$anonymous$$d of transform, may help, or not..
player1.rotation = Quaternion.identity;
player1.velocity = Vector2.zero;
player1.angularVelocity = 0;
player2.position = player2Pos;
player2.rotation = Quaternion.identity;
player2.velocity = Vector2.zero;
player2.angularVelocity = 0;
}
}
Hey, sorry for being late, I tried your code and idk why this started happening: https://www.youtube.com/watch?v=tg93SqlAVy$$anonymous$$
As you can see the player characters are kinda repositioning after scoring but quite worse than before.
But i a had the idea of in the start of every round, instantiating a collider in front of the players so it should work as a wall that stops them from leaning forwards when they get teleported, and its working nicely until now.
Thanks for the help, and for tidying up the script for the newbie here :)
Your answer