x


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

more ▼

asked Apr 27 '11 at 08:25 PM

fninetwo gravatar image

fninetwo
1 2 2 3

If you want to use translate, remember to mark the rigidbody as kinematic.

Apr 27 '11 at 08:30 PM Justin 4
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
more ▼

answered Apr 27 '11 at 09:02 PM

AngryOldMan gravatar image

AngryOldMan
2.5k 12 21 47

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 27 '11 at 08:39 PM

Ben Holmes gravatar image

Ben Holmes
298 2 3 11

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?

Apr 27 '11 at 08:43 PM fninetwo

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.

Apr 27 '11 at 09:45 PM Ben Holmes

Thanks, I got it working with a CharacterController. Does it make sense to use a CharacterController with a space ship, though?

Apr 27 '11 at 11:23 PM fninetwo

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.

Apr 27 '11 at 11:41 PM Ben Holmes
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2492
x1870
x1788
x1692
x1279

asked: Apr 27 '11 at 08:25 PM

Seen: 5356 times

Last Updated: Apr 27 '11 at 08:25 PM