- Home /
How to rotate a camera locally around target?
I have seen only examples of camera orbiters where the camera is being rotated on the XZ-plane. But how I can orbit the cam around based from the rotation of the target (Local instead Global Rotation) ?
This is a little visualization I have mashed up from Unity to help you understanding. The camera moves on the sphere, constantly looking at the target (center of the sphere).
Lets assume I want to move my mouse left/right.
On the left side of image:
Normal Behaviour of most cam-orbit scripts, orientating on the green circle parallel to the ground (global Y).
On the right side of image:
This is in what angle the cam should go around. It follows the green circle, which is obviously the Y-Axis of the target.
The same for up/down on the red circle which is representing the targets X-Axis, instead of the global X.
This is my approach so far:
void LateUpdate () {
if (!target)
return;
x += Input.GetAxis("Mouse X") * xSpeed * distance * Time.deltaTime;
y -= Input.GetAxis("Mouse Y") * ySpeed * distance * Time.deltaTime;
Quaternion rotation = Quaternion.Euler(y, x, 0) * target.rotation;
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*distanceMin*3f, distanceMin, distanceMax);
Vector3 position = -Vector3.forward * distance * rotation + target.position;
transform.rotation = rotation;
transform.position = position;
}
I hope this is clear enough to understand ;) Thanks in advance!
I have a simple asset on the store that does what you are asking if I understand your question correctly. $$anonymous$$essage me on the Unity forum or send an email to joecensoredgames@gmail.com and I'll send you a free voucher for it. Then you can take a look at what the code does, take it apart, use it as is, whatever you would like.
For some reason Unity answers doesn't have a private message feature or I'd send you the voucher here.
Answer by Zilppuri · Apr 19, 2017 at 06:14 PM
Would it work if instead of Vector3.forward, you use the targets forward vector?
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = -target.forward * distance * rotation + target.position;