- Home /
Collisions when pushing object
Hi,
Im having some problems with collisions hope someone can help!
I have a gameobject which i instantiate and the user can move around with the arrow keys, which I have coded as below:
if (Input.GetKey(KeyCode.UpArrow)) transform.Translate(new Vector3(0,0,1) * Time.deltaTime, GameObject.Find("Main Camera").transform);
Im having problems that when the item is being moved around it is going through walls etc, I have a rigidbody and colliders on the walls and the moving object. Clearly im doing something wrong, can someone tell me what im doing wrong?
This also appears to be happening if I use rigidbody.Addforce, with the added fun that doing it that way means i cant stop it moving!
Where are the brackets in that if statement? The whole thing you've written there, scratch it. If it needs to collide with walls, then yes you should be using the rigidbody; just figure out more clearly what force you need to add to it and when.
Transform.translate doesn't care about physics. Use AddForce and AddTorque if you want your rigidbody to interact with walls and floors.
@Unitard you dont NEED brackets on an if statement you can indent the next line and it works fine.
You don't need them, but they're still good to help make your code more readable.
Answer by Berenger · Mar 07, 2012 at 05:47 PM
As said in comments, Transform modification don't care about physic. Use AddForce (and co) or a character controller.
The main cam is accessible through Camera.main, instead of using Find, which can give you performance loss with a lot of objects.
Use Vector.forward instead of new Vector3(0,0,1). It's not important, but it makes the code more clear.