I haven’t found a way to do this yet, so it’s probably my math skills sucking it up.
Quick background:
In my game project, I move the mouse back and forth (along the X axis or Y axis) to slowly rotate an object on the screen. This part works beautifully…
But if I position the camera so that it is looking at the object from a different angle, I always want the rotation to be in-line with the view of the object. For example, If I move the mouse to the right, the object should rotate around the axis that doubles as my forward vector for the camera.
Any idea on how I might accomplish this? Please feel free to be as specific as possible since I have a little harder time with Quaternions. THANKS!
Please be more specific, I’ve don’t understood very well what do u want to do…
Do you want to set the rotation of that object as the camera???
Do you want to rotate that object with the camera?? Set the camera as parent of that object
Do u want that object look the camera?? Use transform.LookAt(Camera.main);
Let me try to be more specific… the player has NO control of the camera at all. The camera moves along in a scripted fashion. The player only controls the rotation of the object that is on the screen that is viewed through the camera.
Since the camera will be changing position (and therefore, the angle that the object is viewed at from the camera), I want the rotation to work in the expected manner. Does this help? If not, I can draw some pictures or something.
it depends what you already have in place to rotate this object.
I think if you have it working in one scenario, the easiest way to extend it would be to use a pivot (empty game object) oriented in the way that works in your scenario (maybe aligned with camera’s y-axis), then parent the object you want to rotate to the pivot. Rotate the pivot. Restore the parent of the object.
You can also convert to camera camera’s coordinate and back, or multiply by quaternions (built from angle and axis) but I think using a pivot is conceptually simpler.
I very likely misunderstood what it is you want, but you solution seems a bit over-complicated. What understand you want is to rotate an object around a position and two directional vectors - based on some input. Correct?
If that is the case, Transform.RotateAround takes a position, a directional vector and a measure of degrees. Two calls with the same position, two different axis and the measure of degrees depending on your input seems to be what you want.
Normally, the two axis would be constant - for instance Vector3.up and Vector3.right. But since you want this relative to a moving camera, you need to figure out what a constant directional vector relative to your camera is in world space.
This is what Transform.InverseTransformDirection is for. It takes a direction which is relative to your transform and gives you a directional vector which is equivalent - in world space.
So in your case, for example Camera.main.transform.InverseTransformDirection (Vector3.up); returns the world space directional vector of the up vector, relative to the main camera. This is what Transform.up is a shorthand for, so unless your direction is not covered by a shorthand, you should use those in stead.
Hope you find this useful for reducing your code complexity and improving performance and understanding.