- Home /
How can I rotate RigidBody and not violate physics?
Hi. I'm trying to create a game that we have a falling ball and an spinning object that spin based on user mouse position. user should rotate the object and hit the ball on time to throw the ball into sky.
The problem is that if user move his finger too fast. the game object moves too fast and goes past through the ball. This is my code so far:
void Update () {
if (Input.GetMouseButtonDown(0)) {
startPos = Input.mousePosition.x;
startRot = obj.transform.localEulerAngles;
lastDist = 0;
}
if (Input.GetMouseButton(0)) {
float dist = Input.mousePosition.x - startPos;
obj.MoveRotation(Quaternion.Euler(startRot + new Vector3(0, 0, -dist)));
for (int i = 0; i < obj.transform.childCount; i++)
obj.transform.GetChild(i).GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(startRot + new Vector3(0, 0, -dist)));
}
}
How can I achieve this behavior for my game. The user should be able to spin the game object as fast as possible.
Thanks in advance.
Answer by tcjulian · Dec 11, 2017 at 09:53 AM
To rotate a rigidbody without violating physics, you'll want to use Torque, which is the rotational equivalent of Force (see https://en.wikipedia.org/wiki/Torque).
In Unity, you should handle any continuous physics adjustments in FixedUpdate
instead of Update
. To add Torque to a`Rigidbody`, use https://docs.unity3d.com/ScriptReference/Rigidbody.AddTorque.html or https://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeTorque.html .
thanks for you answer. This AddTorque method not working with mouse position. It is really important that user can rotate the rigid body with dragging mouse over the screen. How can i do that?
You can base the torque on the distance and direction of the mouse from the object, or on the angle between the object's facing vector and the vector from the object to the mouse. You'll probably want to project the mouse position into world space first with https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html .
In this case. if the user drag the mouse for example for 100 pixels and stop. It will continue rotating and it is not the desired behavior for the drag method.
Answer by othorn · Feb 21, 2020 at 09:38 AM
Use AddRelativeTorque() method. This is an example that rotates an object by adding the force. Unity's physics engine takes care of the rest, no need to run anything in Update().
// In your Start() method
Rigidbody rigidbody = GetComponent<Rigidbody>();
//Add this in your method that is called once
// Set a random rotation, because why not
Vector3 randomRotation = new Vector3(Random.Range(30f, 60f), 0, 0);
// Apply the rotation to the rigid body
rigidbody.AddRelativeTorque(randomRotation, ForceMode.Impulse);
Answer by Fortochka · Jun 04, 2021 at 06:37 PM
How do I set the z value for an object instead of giving the momentum to rotate using RigidBody2D physics? In the sense that angularVelocity
, AddTorque
sets the pulse, and not the angle to be rotated. If you use rotation
, MoveRotation
or transform.rotation.z
, then the player crashing into an obstacle will not rotate, adjusting his position under the wall. And if I don't set the rotation, but just make the player go into the wall at a slight angle, then he will just be able to turn himself and adjust to the wall. Help please, of course, if what I want is possible at all
Your answer
Follow this Question
Related Questions
How to make a dynamic bow shot and the arrow to curve and turn correctly when shot? 0 Answers
transform.Rotate and Rigidbody.MovePosition 1 Answer
How to limit the motion direction of a GameObject? 1 Answer
how to CORRECTLY position and rotate a gameobject in unity 2 Answers
Rotate using physics 6 Answers