- Home /
Trouble with Panning when camera is Orbited?
I have been all over the forum and Scripting reference trying to solve this issue. Sovacs1 wrote a great answer about camera movements a few years ago. He mentioned the problems that come from calculating the axis when the camera is orbited but I don't really understand his solution or how to implement what he explains in the post.
The trouble is I have a camera that is aimed at an object, I rotate around the object and zoom in and out. I also want to pan (or more correctly truck)the camera side to side and pedestal the camera up and down.
I can get the basic effect with:
//Previous omitted code handles Zoom and Orbit based on the MouseOrbitZoom script
else if (Input.GetMouseButton(1))
{
targetOffset.x += (transform.right * -Input.GetAxis("Mouse X") * panSpeed).x;
targetOffset.y += (Vector3.up * -Input.GetAxis("Mouse Y") * panSpeed).y;
}
//Additional code clamps the Orbit so the model wont spin head over heels.
The rest of my code is a modification of a script from the wiki adding ease in and out of camera movements.
The trouble is that once you rotate the camera 90 degrees to look down at the object from above, your pedestal movement ignores the cameras rotation and acts on a world space rather then a local or perhaps Space.self causing the movement to look more like zooming in and out. Also the Trucking movement continues to move in the worlds x rather then recognizing the cameras new rotation.
I'm trying to make the camera Truck and Pedestal based on the cameras left and right and the cameras top and bottom rather then the worlds east and south and up and down as it were.
I would also like to clamp the side to side and up and down movement based on the model if possible or predefined limits similar to the way the wiki script clamps the rotation.
//Clamp the vertical axis for the orbit
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
Any Ideas of How I might go about doing this?
note: All the code is in C#
Is there an object that the camera is rotating around or parented to? You could try just lerping the position to one with a higher y value for it to move up and down at least.
Answer by whebert · Apr 05, 2013 at 01:53 AM
I believe something like the following might do what you want, that is if I understood you correctly; you wanted to be able to Truck and Pedestal relative to your camera's orientation, not in world space? And you also wanted to clamp the Truck/Pedestal's movement by some distance?
using UnityEngine;
using System.Collections;
public class OrbitCamera : MonoBehaviour {
public Transform target;
public float maxOffsetDistance = 20f;
public float orbitSpeed = 15f;
public float panSpeed = .5f;
public float zoomSpeed = 10f;
private Vector3 targetOffset = Vector3.zero;
// Use this for initialization
void Start () {
if(target != null) transform.LookAt(target);
}
// Update is called once per frame
void Update () {
if(target != null)
{
Vector3 targetPosition = target.position + targetOffset;
// Left Mouse to Orbit
if(Input.GetMouseButton(0))
{
transform.RotateAround(targetPosition, Vector3.up, Input.GetAxis("Mouse X") * orbitSpeed);
float pitchAngle = Vector3.Angle(Vector3.up, transform.forward);
float pitchDelta = -Input.GetAxis("Mouse Y") * orbitSpeed;
float newAngle = Mathf.Clamp(pitchAngle + pitchDelta, 0f, 180f);
pitchDelta = newAngle - pitchAngle;
transform.RotateAround(targetPosition, transform.right, pitchDelta);
}
// Right Mouse To Truck, Pedestal
if(Input.GetMouseButton(1))
{
Vector3 offset = transform.right * -Input.GetAxis("Mouse X") * panSpeed + transform.up * -Input.GetAxis("Mouse Y") * panSpeed;
Vector3 newTargetOffset = Vector3.ClampMagnitude(targetOffset + offset, maxOffsetDistance);
transform.position += newTargetOffset - targetOffset;
targetOffset = newTargetOffset;
}
// Scroll to Zoom
transform.position += transform.forward * Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
}
}
}
I had the same problem, thanks a lot for the help!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613095723im_/https://answers.unity.com/themes/thub/images/avi.jpg)