- Home /
Best way to rotate a physics-object based on input
I am trying to rotate a certain object which has a rigidbody and some colliders. The rotation is based on user input and must be applied directly and always the same (i.e. it should not be influenced by other physics objects).
What I tried:
- Make the object kinematic and change it's rotation every frame -> fail, because of tunneling
- Make the object kinematic and change it's angular velocity every frame -> fail, kinematic objects cannot have velocity (that is weird, because this is the way I do it in other engines, like Box2D)
- Keep the object dynamic (not kinematic) and change it's angular velocity every frame -> fail, because other objects have influence on the final rotation.
- Attach a configurable joint to it and change it's target rotation every frame -> fail, because other objects have influence on the final rotation.
- Attach a configurable joint to it and change it's angular limits every frame -> fail, because other objects have influence on the final rotation.
- Make the object kinematic, attach a configurable joint to it and change it's target rotation every frame -> fail, because the joint drivers don't work on kinematic objects.
So, I'm kinda out of ideas here. I hope someone has a nice suggestion?
Answer by Mystyr Nile · Oct 11, 2010 at 09:40 PM
Here's a JavaScript I made that should work when attached to a kinematic rigidbody.
var speed=90; //The above defines the speed at which this object will rotate when the //x axis is not zero.
function Update(){ var x=Input.GetAxis("Horizontal")*Time.deltaTime*speed; transform.Rotate(0,x,0); } //The above rotates this object at the speed of "speed" degrees per //second in the direction pressed.
That's the same as changing the rotation directly. The problem is objects fall through the object that is rotated, when the angle is big enough (called tunneling).
I . . . don't understand the concept of tunneling. Could you explain more deeply?
Answer by dissidently · Dec 20, 2010 at 05:36 PM
You can place the object on a unique layer, this way no other physics objects will have influence over it if you choose to turn that off in that layer. Then do as you please with it.
If you need it to feed information about it's position to other objects for their physics, create a dummy object in the right layer for objects you want to influence, turn off the dummy object's rendering, but add joints or nest objects or whatever you need do, then get the transform information regularly from your player controlled object and pass it to the dummy object as regularly as needed.