Camera pan problem

ive added some code in and now ive got it so when you right click you can pan around the moving object and when your release it snaps back to the original start but i want the cam to snap back to the original view on the object instead of the objects starting point this is the new updated code

var target : Transform;
var distance = 5.0;
var xSpeed = 125.0;
var rightclicked : boolean = false;
var originalPosition : Vector3;
var originalRotation : Quaternion;

private var x = 0.0;

@script AddComponentMenu("Camera-Control/Mouse Orbit")


function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    target = transform.parent;
    originalPosition = transform.position;
    originalRotation = transform.rotation;
}

function Update (){  
  rightclicked = Input.GetMouseButton(1);
}
function LateUpdate () {
    if (target && rightclicked == true) {
        x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
        var rotation = Quaternion.Euler(0, x, 0);
        var position = rotation * Vector3(-1.0, 2.5, -distance) + target.position;
        transform.rotation = rotation;
        transform.position = position;
    }else if (target && rightclicked == false) {
        transform.position = originalPosition;
        transform.rotation = originalRotation;
      
    }        
}

var originalCamPosition : Vector3;
var originalCamRotation : Quaternion;

void Update() {
   if(!rightClicked && Input.GetMouseButtonDown(1)) {
       rightClicked = true;
       originalCamPosition = camera.transform.position;
       originalCamRotation = camera.transform.rotation;
   } else if (rightClicked && Input.GetMouseButtonUp(1)) {
      rightClicked = false;
      camera.transform.position = originalCamPosition;
      camera.transform.rotation = originalCamRotation;
   }
}

I think you want something like this. It will snap the camera back immediately, so you may want to use Vector3.Slerp to make this a smooth transition, but you get the gist.

ive added some code in and now ive got it so when you right click you can pan around the moving object and when your release it snaps back to the original start but i want the cam to snap back to the original view on the object instead of the objects starting point
this is the new updated code

decided to just work this the code i have and work around it :slight_smile:

After some exchange in the comments section. this solution was settled on:

https://github.com/phodges/Unity/tree/master/Questions/335949

The model is a little different from the author’s, as it smooths position and rotation. Here’s a little setup information, for those that want to use the code:

You need to create a separate magnet gameobject. Do that and put my script onto it. In that script, assign the ‘Target’ property to be whatever it is you want the object to track and rotate around. Once you’ve done that, assign the ‘Attract’ property to be the object you want to have brought to the magnet point; this would be the camera in this case.

There are a load of properties you can tweak and adjust. It’s all commented, so you ought to be able to see what’s going on.