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
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.
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.
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);