- Home /
How can I move an object without translate or addforce?
I need to move a platform in a 2d game however translate makes it go through my CharacterController player if I move the platform up. The platform is not a rigidbody as I don't like the smooth acceleration and deceleration they have. I have seen scripts where they move objects with transform.position but I can't find any. I need it to move in the same way that translate makes it move.
Answer by robertbu · Jul 02, 2013 at 11:23 PM
Moving your platform using transform.position is likely to have the same problems as transform.Translate(). To get rigidbody movement without the smooth acceleration of AddForce() just assign the velocity directly. For example to go in the up direction:
rigidbody.velocity = Vector3.up * speed;
This will cause an immediate jump to the specified velocity without any acceleration. If you want this velocity to continue even after other forces come into play (such as a collision with another Rigidbody) you can assign it in FixedUpdate().
While this does work it moved up for half a second and then it began to fall down. I locked the rotation cause it would rotate when my player would land on it. I don't understand why it falls or rotates if my player doesn't have a rigidbody. Btw I put the code in fixedupdate cause I read that when an object has a rigidbody attached you use fixedUpdate ins$$anonymous$$d of update.
Without a clear understanding of the nature of your platforms and the player, I cannot say what is going on. Note by default gravity is turned on for Rigidbodies, so your platform will fall if the force or setting the velocity is removed. In addition if you want to have the Rigidbody stop, you can set the Is$$anonymous$$inematic flag to true.
I turned gravity off. I also noticed that when its moving up my CharacterControllers collider is going through the platform somewhat.
$$anonymous$$y player has a CharacterController attached and the platform is simply a box collider, not a trigger, rigidbody with no gravity and not kinamatic.
Answer by markpdolby · Jul 02, 2013 at 11:18 PM
Try using Vector3.MoveTowards to move your object, the docs have an example of how to use it here: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.MoveTowards.html
Answer by Radon · Jul 02, 2013 at 11:19 PM
I always use the following script to change the location of an object:
transform.position = Vector3(X, Y, Z);
make sure to change the X, Y, and Z terms to where you want your object to be transformed.