Apply a rotation AND a torque at the same time ?
Hi, I have an issue since a while now.
My player have a rigidbody. I'm making him lookaround with the mouse and transform.Rotate(). And in my game there is an event that apply a torque with rigidbody.addTorque that make the player rotate. In the editor updates and fixedUpdates are synchronized and both can be applied at the same time, works fine.
But when I build, Updates are way faster than fixedUpdates. So when I try to apply both at the same time the transform.Rotate() 'overwrite' the addTorque. I tried a coroutine 'waitForFixedUpdate' for the Rotate(), now both work at the same time in the build, but when I look around with the Rotate() it's jittering, not smooth at all. Anyone could help me please?
Answer by streeetwalker · Apr 26, 2020 at 02:02 PM
@Tritize, It's not exactly clear in your post but If you are using Righdbody Physics do not also attempt to manipulate the transform properties of the object
Note that all input code must go into an Update event handler to work properly and always use FixedUpdate for physics related code for it to work properly.
You may Use and of Rigidbody rotation methods to try to rotate an object to look at something
Another way is add your object as a child of the Rigidbody - that is, apply a Rigidbody to the an empty game object and give it a collider. Remove the Rigidbody and collider from your game object and add it as a child of the empty parent Rigidbody object. You can then freely and rotate child transform without any conflict.
You can try to synchronize Update and FixedUpdate, but you have to be aware of how much your code is trying to do with physics. I recommend reading this article: https://johnaustin.io/articles/2019/fix-your-unity-timestep
I am using rigidbody physics for everything except to orient the view of the player. It's an FPS and I'm using rigidbody.transform.Rotate() to make the player look at where I point with the mouse. And that's what is causing a conflict with an addTorque. So if I don't use the transform.Rotate() like you suggest I have to orient the view of the player using Torques ? Seems difficult to achieve, how should I do this ? :/
@Tritize, It wasn't clear in your explanation if the code was already applying another torque to get the Rigidbody to move. If you are then it would seem the only way to independently look at a target is to use the alternate method I wrote about
If you are not applying a torque for motion, then you don't have to apply torque to rotate an object.
You can use Rigidbody.rotate and Rigidbody.$$anonymous$$oveRotation to set rotation values directly. If you have used Rigidbody.AddForce or some other Rigidbody method to move your object, setting the rotation directly through those functions will not affect the motion as long as the center of gravity (center of mass) is in the center of the object.
(if you have some odd shaped mesh collider on the object, that may be a consideration, as the collider defines the volume of the physics mass and center of gravity may not be what it seems. You'd have to try it!)
$$anonymous$$y mesh collider is a simple sphere. Yes I'm applying a torque with my code. Which is the alternate method in your previous post?
edit : put a child that is affected by torque while parent is affected by transform.Rotate() ?
Ok I tried your alternative way. I have the parent with rigidbody and the child that I transform.rotate.
Problem is, when I apply a torque to the parent, I don't have the same behavior than before at all. $$anonymous$$aybe it's because the rigidbody axis are not aligned anymore with the ones of the child? And even by adding a torque on the rigidbody like addTorque(child.transform.forward...) it does a rotation without any sense...
Yeah when I do addTorque(child.transform.forward) the rigidbody rotate around its own forward.. and since its not aligned it does nonsense. Any idea how to correct this ?
First, you must not have a Rigidbody or collider on the child - it will cause trouble. You have to be specific about the behavior. Yes, you would need to make sure your object is centered. But if the child does not have a rigid body or collider it will not affect the behavior of the parent rigidbody in any way other than visually if the child is not centered.
I don't quite understand why you are applying a torque force to move the object if you don't want it to look like it is rotating. You want the object to point to the mouse? Then don't use torque to move it. Just AddForce in the direction you want to move. If it's rolling on the ground or against some other object, just remove any angular drag and friction. It will slide with AddForce, and you can use RigidBody.rotate to point where ever you want.
I don't have any collider or rb on the child. child is centered to the parent. But when I apply a torque to the parent, axis of the parent and of the child does not match (I test that with a simple sphere atm).