Like the title says… whenever I start the game with the ball just sitting on the paddle, within 5 seconds it takes me to the game over screen even though nothing touches the box of lose collider.
Hi Mohammad,
Welcome to our community!
Unity’s physic engine is highly optimised meaning it checks the position of a collider occasionally only. You could try to set the Collision Detection mode of the ball’s Rigidbody2D to “Continuous”. In most cases, that fixes the problem.
In our particular case, the problem is that we manipulate the transform.position
via code and override the calculated values of the physics engine. It is likely that the position of the ball does not match the position of the collider anymore.
A better solution would be to disable the physics simulation for the ball while manipulating the position
via code.
void Start ()
{
paddleToBallVector = transform.position - paddle1.transform.position;
myAudioSource = GetComponent<AudioSource>();
myRigidBody2D = GetComponent<Rigidbody2D>();
myRigidBody2D.simulated = false; // <-------- add this
}
private void LaunchOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
hasStarted = true;
myRigidBody2D.velocity = new Vector2(xPush, yPush);
myRigidBody2D.simulated = true; // <-------- add this
}
}
I hope that makes sense.
Did this fix the issue?
See also:
- Forum User Guides : How to mark a topic as solved
Hi,
I had the same issue and I confirm that disabling simulation fixed it.
Thanks !
I get an error message saying that “myRigidBody2D does not exist in the current context.”
Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?