- Home /
Mouse Orbit Reset Camera
Hi
I was wondering, is there a Mouse Orbit script which allows the user to click a button and reset the camera?
I've tried just re-calling the start function but that doesn't seem to work
Here's an example of my code, I'VE ADDED IN THE "ResetCamera" but it doesn't seem to work
var target : Transform;
var targetHeight = 2.0;
var distance = 25;
var maxDistance = 80;
var minDistance = 10;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -150;
var yMaxLimit = 100;
var zoomRate = 20;
private var x = 0.0;
private var y = 0.0;
private var initialPosition : Vector3;
private var initialRotation : Quaternion;
@script AddComponentMenu("Camera-Control/MouseOrbitZoom")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
initialPosition = transform.position;
initialRotation = transform.rotation;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function ResetCamera () {
transform.position = initialPosition;
transform.rotation = initialRotation;
print(transform.position.x);
}
function LateUpdate () {
if(!target)
return;
// If either mouse buttons are down, let them govern camera position
if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
}
distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);
distance = Mathf.Clamp(distance, minDistance, maxDistance);
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation:Quaternion = Quaternion.Euler(y, x, 0);
var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
transform.rotation = rotation;
transform.position = position;
print(transform.position.x);
}
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);
}
Thanks Sam
same question I think perhaps because it's late update, it doesn't listen to OnGUI.
what about having it use a "GetkeyDown" using a key code ins$$anonymous$$d of an OnGUI. is that effected by the late update as well?
it seems the code needs to effect desiredRotation and desiredDistance or the reset will be overwritten the frame after the key is pressed.
I can get the camera to rotate back for one frame with
private Quaternion intCamRot = Quaternion.Euler(0.0f,1.0f,0.0f)
void Start()
{
intCamRot = transform.rotation;
}
void ResetCamera()
{
transform.rotation = intCamRot;
}
but the position is not effected with
private Vector3 intCamPos;
void Start()
{
intCamPos = transform.position;
}
void ResetCamera()
{
transform.position = intCamPos;
}
and the effect or the rotation only lasts one frame, likely do to the late update as ZoomDomain suggests.
I have looked at Pedagogic368's code but have not been able to convert the solution over to the $$anonymous$$ouseOrbitZoom Camera.
Anyone else have any ideas or a working reset button?
Do I need to reset/reload the entire scene?
a few related posts that have failed to solve this issue:
http://forum.unity3d.com/threads/3891-Reset-camera-to-original-start-point
http://answers.unity3d.com/questions/414919/create-reset-button-to-main-camera.html
http://answers.unity3d.com/questions/56389/reset-camera-position-mouselook.html
The community definitely needs and wants a fix to this issue.
Tested with update and late update. That's not the issue the position and rotation only reset for one frame or while the button is held down. It may be a static variable or a needed Awake() function. perhaps the static ClampAngle is overriding the ResetCamera. I'll keep testing.