- Home /
RigidBody2D velocity not working after returning to Menu and going back to game.
I have a simple PONG game for my phone that I want to make. The pause button allows you to return to the menu and when you return to the menu you can return back to the game. The game and the menu are seperate Scenes.
However when you return back to the game, the ball is stuck in the middle and the velocity of the rigidbody isn't doing anything.
I can see the rigidbody2d's velocity not at 0,0 but it isn't doing anything to move the ball.
public class BallBehaviour : MonoBehaviour {
public TextMeshProUGUI rightScore;
public TextMeshProUGUI leftScore;
public GameObject lGoal;
public GameObject rGoal;
private BoxCollider2D right;
private BoxCollider2D left;
private int rAScore; //actual score
private int lAScore;
private Vector2 lasPos;
// Use this for initialization
void Start () {
Random.InitState(Mathf.RoundToInt(Time.time));
Reposition();
right = rGoal.GetComponent<BoxCollider2D>();
left = lGoal.GetComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update () {
if (right.IsTouching(gameObject.GetComponent<CircleCollider2D>()))
{
//Debug.Log("right");
rAScore++;
rightScore.text = rAScore.ToString();
Reposition();
}else if (left.IsTouching(gameObject.GetComponent<CircleCollider2D>()))
{
//Debug.Log("left");
lAScore++;
leftScore.text = lAScore.ToString();
Reposition();
}
StartCoroutine(ScanPos());
}
public void Reposition()
{
Random.InitState(Mathf.RoundToInt(Time.time));
int r = Random.Range(1, 3);
gameObject.GetComponent<Rigidbody2D>().MovePosition(new Vector3(0,0,0));
if (r == 1)
{
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(7, 5f);
}
else
{
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(-7, -5f);
}
}
IEnumerator ScanPos()
{
yield return new WaitForSecondsRealtime(0.1f);
if (lasPos == null)
{
lasPos = gameObject.GetComponent<Rigidbody2D>().position;
}
else if (lasPos == gameObject.GetComponent<Rigidbody2D>().position)
{
Reposition();
}
}
}
You can also see that I have tried to fix it with a coroutine, but I'm not too sure on how coroutines really work for Unity and I'm most likely not using the coroutine efficiently.
The coroutine isn't even working correctly so for the time being I have it commented out.
Yeah, that coroutine isn't going to be any good. It's called every frame with a 0.1f delay. Basically, it's going to create a ton of those things and cause all kinds of chaos.
How are you pausing the game? Setting the Timescale? If you are, are you sure you're setting the timescale back to 1?
Yes, the pause menu is paused through time scale. That may be my problem, lemme check
Your answer
![](https://koobas.hobune.stream/wayback/20220612183123im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnCollision2D stopped working 1 Answer
Optimizing C# Code and physics 2 Answers
Child object not moving with parent. 2 Answers
Trying to make a bounce platform that tosses the player. 1 Answer
Transform.forwrard of a Sprite? 1 Answer