- Home /
rotate around with physics
Rotation issue.
trying to create halo forge like functionality to rotate a selected object. If I move the mouse up i'd like the bottom of the object to come towards the camera and the top to go away. Globally that is decrementing the Z axis rotation, but i'd like it to apply torque relative to the cameras Z axis, not its own local or global. I thought maybe using camera.transform.transformdirection might do it but I can't seem to implement it even though I feel like it should work in principle, Maybe transformdirection is wrong and i need some kind of transformrotation function?
I saw this solution
http://answers.unity3d.com/questions/133444/rotate-an-object-relative-to-a-camera.html
however I want to use rigidbody not transform so that if I rotate into another object it will collide.
I've honestly been trying for a few hours now to hammer out a solution and its not coming to me. Help would be much appreciated. Thank you.
Answer by Sisso · Oct 11, 2012 at 02:41 AM
To rotate a RigidBody you must apply Torque Force (http://en.wikipedia.org/wiki/Torque).
Will be some complications when you try tu control a object rotation using forces cause its inercia. Take a look on this thread, implement and use a PID controller is easier than it seem.
http://answers.unity3d.com/questions/197225/pid-controller-simulation.html
Editted:
A sample code that I think that rotate an object in Y axis using torqueForce. It must be tested and expanded to X/Z axis. I have simplified the PID controller by a simple weight.
var weightDistance = 0.1;
var weightSpeed = 0.1;
var angularDistanceForTarget = Vector3.Angle(targetDir, forward);
var angularVelocity = rigidbody.angularVelocity.y;
var mod = (angularDistanceForTarget * weightDistance) - (angularVelocity * weightVelocity);
var force = maxTorqueForce * Mathf.Clamp(mod, -1, 1);
rigidbody.AddTorque (Vector3.up * force);
Yea its annoying that there isn't a kind of simple physics which allows you to move objects without forces but with colliders.
I think though i'm just going to turn freeze/iskinematic off and on quick to remove applied forces. Your essentially constructing stuff just like forge. Its seperate from the game itself so i'm not worried about the implications of things changing there states.
The problem is that if you want to simulate the real world, you must to use real world rules. :P I have edited my answer added a sample pseudo code.