- Home /
Mouse Orbit after changing Position sets camera back to last position
Hi,
I am really stuck on this code and I know I am doing something stupid. I have a camera that is is getting the mouse input and a target to use for a mouse orbit. There is another script on objects that when clicked sets a flag for moving the camera with a lerp to the object.
This all works fine except when I try and do a manual mouse orbit again because it snaps the camera back to the last position I was at during the last mouse orbit.
I have tried to offset it and a couple of other stabs at the dark but having no luck so have given up. It would be great if someone could give a hand and I do hope that all makes sense.
Edit: Also this is it running which will do a better job explaining it. http://3dsteve.co.uk/camerawebbuild/camerawebbuilt.html
function LateUpdate ()
{
if(Input.GetMouseButton(0) && !selectanatomy.moving && GUIUtility.hotControl == 0)
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0.0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.transform.position;
transform.rotation = rotation;
transform.position = position;
}
if(selectanatomy.moving) //object clicked sets this to true
{
transform.position = Vector3.Lerp(transform.position, targetposition.transform.position, Time.deltaTime * 5);
renablecamera();
}
}
function renablecamera()
{
yield WaitForSeconds (1.0);
//I THINK THIS IS WHERE I NEED TO RESET THE ROTATION OF THE CAMERA TO A NEW X AND Y POSITION BASED ON WHERE THE TARGET POSITION IS
selectanatomy.moving = false;
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Answer by Melih Şimşek · Dec 17, 2013 at 03:52 PM
In Start() function, you have to get Camera Original Rotation:
origintalRotation = transform.localRotation;
Then,
In Update() function, set the rotation to the Camera
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationy, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion
Thanks very much for the above code, it seems like it should work but it sadly did not do the job.
It may be worth noting that the script is attached to an empty gameobject and the camera has a smoothfollow on it with that object as the target.
Your answer
Follow this Question
Related Questions
Camera (mouse orbit), linecast and shakiness 1 Answer
Rotate around object at a given angle 2 Answers
How to make camera position relative to a specific target. 1 Answer
camera slides and bounces while moving C# 1 Answer
Camera animation issue 0 Answers