- Home /
Rigidbody not colliding with walls
So, I made a dx ball type game and in the game I added four walls(not rigidbody) as boundaries. But the problem begins when I test the game the ball(rigidbody) bounces off when it strikes the walls but the paddle(rigidbody) passes through the wall when I move it there. My paddle control script-->
private var ray : Ray;
private var hit : RaycastHit;
function Update () {
transform.Rotate(0, 5, 0);
if(Input.GetMouseButton(0)){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit)){
transform.position.x = hit.point.x;
}
}
}
Answer by Rett_ · Mar 13, 2013 at 05:13 PM
Modifying transform.position of a rigidbody directly is in general a bad idea, because it does not apply physics simulation (which includes collision detection).
Ideally, you only want to move rigidbodies around using AddForce. It guarantees the most realistic physics simulation, but can get a bit complicated when you try to move to a specified point in space and stop.
You can control your rigidbody more directly by modifying its velocity vector. For example:
private void FixedUpdate ()
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float velocityX = mousePosition.x - transform.position.x;
rigidbody.velocity = new Vector3(velocityX * MOVEMENT_SPEED, 0, 0);
}
will move your paddle towards the mouse cursor (assuming orthographic view) on the X axis . Paddle speed will be proportional to mouse - paddle distance.
Finally, you can move your rigidbody around using MovePosition method, which simply "teleports" your object and performs collision checks.
umm.. what is $$anonymous$$OVE$$anonymous$$ENT_SPEED