- Home /
MouseOrbit script in alternate space
Unhappy with my previous results I am now using the MouseOrbit script from the wiki.
Unfortunately my environment differs a bit from the assumptions in the script, in that the view is always from the top (e.g. Z = -50) on the X/Y grid (RTS style). The script assumes that one is operating in an environment where one is looking towards Z.
I don't know why but for the love of Christ I cannot adapt the script to work in my alternate space... it doesn't seem to be as simple to just change the axes - the rotations are all screwed up... any ideas?
Here the script:
x += Input.GetAxis("Mouse X") * 10f * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * 10f * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.transform.position;
transform.rotation = rotation;
transform.position = position;
Answer by Bunny83 · Oct 28, 2012 at 12:01 AM
Well, Unity's usual space is like this. It uses a left-handed system where +z is forward +x is right and +y is up. You're of course not bound to this, but it simplifies a lot when you stick to this system. So for an rts game you would use the x-z plane as ground.
However, it your game is already setup with x-y as ground and -z as up (+z down), you can juse change:
transform.rotation = rotation;
to
transform.localRotation = rotation;
and parent this object to an empty gameobject which is rotated downwards (x rotation == 90/-90).
Hey thanks for this, but I tried a couple of different ways around this and I couldn't find a way to make it work. It does work however, if the object is at Vector3.zero, but that is not likely to be the case in a non-staged environment. I created a test project that recreates this exact case. If possible, could you kindly have a look at it? http://dl.dropbox.com/u/4674785/Test.zip
Scene 1 is the default case, where you can see that everything works nicely. Scene 2 is my case with axes switched and the camera attached to a rotated empty object. As soon as the target object is not at Vector3.zero anymore, the whole script ceases to work... any idea what I did wrong?
Your answer
![](https://koobas.hobune.stream/wayback/20220613082953im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Why the control with gyroscope inverted when I make 180 degrees turn? 1 Answer
Help with camera coding - Modern Zelda-style camera 1 Answer
Keyboard Controlled Overhead Camera Rotation 0 Answers
Very weird rotation axis bug 1 Answer