- Home /
How to properly move a rigidbody/collider?
Hello everyone,
I wanted to ask what is the best way to move a rigidbody with a collider? I have many problems using AddForce or MovePosition so I would like to just use Translate for all the movement. I heard that this is bad for the collision detection and performance but I also heard that it doesn't matter. I would really like a reply because if it's indeed bad to use Translate, then I would have to rewrite everything.
Thanks
If you want to use translate, remember to mark the rigidbody as kinematic.
Answer by AngryOldMan · Apr 27, 2011 at 09:02 PM
rigidbody.velocity
is how we moved our rigidbodies
//Set moveDirection to the vertical axis (up and down keys) * speed
moveDirection = Vector3(MoveSpeed*Input.GetAxis("Horizontal"),-Gravity,MoveSpeed*Input.GetAxis("Vertical"));
//Transform the vector3 to local space
moveDirection = transform.TransformDirection(moveDirection);
//set the velocity, so you can move
transform.rigidbody.velocity.x = moveDirection.x;
transform.rigidbody.velocity.z = moveDirection.z;
rigidbody.AddForce (Vector3.up * -10);
Wouldn't this cancel out the physics of the script holder? The velocity is always going to be moveDirection even if it's hit by another object with a collider.
Answer by Ben Holmes · Apr 27, 2011 at 08:39 PM
if you're using a CharacterController then Move() works well. Otherwise you can use Translate but you'll have to handle collisions yourself meaning it won't stop you from moving the object through other objects unless you implement that code yourself.
Thanks, I don't use a CharacterController, I use a Box Collider and Translate. I don't need to stop on collision or anything but I do need to call a method on collision and when using kinematic, it doesn't register OnCollisionEnter. Any help with that please?
For OnCollisionEnter to work the object which is entering the collider needs to have a rigidbody attached to it. You could also just use Physics.OverlapSphere every frame to check the area around the object (which only requires a collider) but if you do this with a lot of objects at once it may have a big effect on performance.
Thanks, I got it working with a CharacterController. Does it make sense to use a CharacterController with a space ship, though?
if it works for your game then yes. If you have a lot of ships which use CharacterControllers then performance may become an issue and in that case I would suggest using a simpler collider and handling some of the collision/physics yourself.
Your answer
Follow this Question
Related Questions
How to setup character Collisions? 2 Answers
OnCollisionEnter but have colliding object not move the object it collides with 1 Answer
Preventing objects from intersecting with minimal physics 3 Answers
Modifying height directly on a Rigidbody Object without gravity 1 Answer
Objects rotating after falling on another object with Physics & Gravity? 1 Answer