- Home /
Advanced Orbit Camera Around Target
Hi there!
I'm trying to figure out how I could make a mouse orbit script like they've done here: http://www.byggplanlegger.no/
This movement differs from the standard Mouse Orbit script in that the camera adjusts its position and rotation smoothly even when scrolling directly over the poles, without suddenly flipping the vectors, turning the camera up-side down or clamping/locking the up and down vector.
I've looked all over the unity forums for a solution with no luck.
Please help me in the right direction. Do I need to apply a matrix transformation of some kind or is there a simpler solution for this?
Thanks! :)
Answer by robertbu · Oct 08, 2013 at 11:12 PM
Here is a bit of code that mimics the behavior of the orbit script at the link. Attach it to the camera and drag and drop the target game object onto the 'target' variable in the inspector.
#pragma strict
var target : Transform;
var dist = 10.0;
var speed = 1.0;
private var euler = Vector3.zero;
private var center : Transform;
function Start() {
// Sets the camera up looking directly forward and looking at the
// object with Vector3.zero rotation
transform.position = target.transform.position + Vector3.back * dist;
transform.eulerAngles = euler;
// Place an empty game object at the pivot of the target and make the
// camera a child
center = new GameObject().transform;
center.position = target.position;
transform.parent = center;
}
function Update() {
if (Input.GetMouseButton(0)) {
euler.x -= Input.GetAxis("Vertical") * speed;
euler.x = Mathf.Clamp(euler.x, -90.0, 90.0);
euler.y += Input.GetAxis("Horizontal") * speed;
center.eulerAngles = euler;
}
}
Thanks, but this will cause the x-rotation to be locked when passing the poles. So this does not mimic what's in the link.
It is not a perfect match, but with the one in the video also cannot be rolled across the the poles. Once you reach a pole view, the object begins to spin on the local 'y' axis. In addition, with the one in the video, there are many times were the mouse movement is non-intuitive. That is the object rotates the opposite way that it did earlier for the same mouse movement. Play with them side by side. Also you can take out the clamp on line 26 to free the movement, but you'll end up with the same backward movement issues that the link app has.
I don't experience any inverting of the mouse movement. I only experience the object rotating on the y-axis when reaching the poles, which is exactly what I want. :)
With these rotation questions, often the hardest part is figuring out the right behavior. I answer a lot of rotation questions on this list, so I have some background in rotations. Let me tell you what I see when I play with the app at your link.
When I first start the app, I see an angle view of a house. Let me label the long side of of that house facing the user at startup configuration the 'back' and the short side then becomes the 'left'. Click and drag horizontally on the house until the either the 'left' side or the 'right' side is facing the user. Click down again and move the mouse up and down. On my machine the house will only rotate through a slight angle and I cannot rotate it to see the top or bottom. The rotation is locked, which to me is very user unfriendly.
Now refresh the web page to get back to the original configuration. Rotate the house slightly to have the 'back' face the user. Click and drag up and down. It works fine and I believe how the user expects. You can see the 'top' and the 'bottom'. Return to the back-on view, and click and move the mouse horizontally until the the 'front' side is facing the user. Click again and move the mouse up and down. Now the house rotates in the opposite direction. That is when the 'back' of the house is facing the user and you move the mouse up, the house rotates to show the 'bottom' as if you grabbed the house and are rotating it. But when viewing the house from the 'front', the house moves in the opposite direction so the movement attempts to show the roof...and opposite from what the user would expect. It is as if you grabbed hold of the edge of a wheel and pulled up only to have the side grabbed go down.
Thanks for all your input, I really appreciate it. And allthough I honestly agree with you I don't really have a choice since the client I'm doing this for has asked specifically for this kind of camera behaviour. Additionally I would really like to know how to do it like in that link just for the sake of learning. Thanks a lot though.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
OnMouseDown on what? Camera Movement Help. 1 Answer
Lock target and orbit it 3 Answers
Reproduce WorldToViewportPoint function 2 Answers
Orbit camer while paused? 1 Answer