- Home /
Reset x and y values on camera orbit.
I have modified the camera orbit script found here: [http://wiki.unity3d.com/index.php?title=MouseOrbitImproved][1] and combined it with the smooth follow technique found in the Mecanim tutorial. Everything works great, except I want my camera orbit x and y values to reset to the camera's current position when in smooth following mode (doesn't have input) so that when it goes back to orbit mode (has input) the camera continues from its current position rather than getting reset. I got it to work in the x axis, but for some reason the y axis seems to be constantly returning the camera to an unexpected angle while it is getting input, which is weird because that line shouldn't even be running when there is input.
Here is the line giving me trouble: y = angles.x; When active this line constantly resets the y value even if there is input value.
The full code is below:
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class CameraOrbit : MonoBehaviour {
public Transform target;
public float distance = 2f;
public float zoom = 5.0f;
public float height = 1.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public float smooth = 3f;
Transform standardPos;
float x = 0.0f;
float y = 0.0f;
// Use this for initialization
void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
standardPos = GameObject.Find ("CamPos").transform;
}
void LateUpdate()
{
Vector3 position;
Quaternion rotation;
// controlls camera distance, cycling through 3 different positions
if(Input.GetButtonDown("Zoom") & distance == 2)
{
distance = 5;
}
else if(Input.GetButtonDown("Zoom") & distance == 5)
{
distance = 10;
}
else if (Input.GetButtonDown("Zoom") & (distance == 10 ))
{
distance = 2;
}
//Get input for camera orbit (assigned to left joystick)
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
rotation = Quaternion.Euler(y, x, 0);
//Moves the camera down for crawl if crawl is active
if(Input.GetAxis("Crawl") <= -.1)
{
height = .5f;
}
else if(Input.GetAxis("Crawl") >= -.1)
{
height = 1;
}
//Apply distance and height variables to camera position
Vector3 negDistance = new Vector3(0.0f, height, -distance);
position = rotation * negDistance + target.position;
//only applies orbit if Mouse (Right Joystick) has input
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") !=0)
{
transform.rotation = rotation;
transform.position = position;
}
//otherwise smoothly go back to origen using camPos object parented to character.
if((Input.GetAxis("Mouse X") == 0 || Input.GetAxis("Mouse X") ==0))
{
transform.position = Vector3.Lerp(transform.position, standardPos.position, Time.deltaTime * smooth);
transform.forward = Vector3.Lerp(transform.forward, standardPos.forward, Time.deltaTime * smooth);
//Reset x and y to current position
Vector3 angles = transform.eulerAngles;
x = angles.y; //This works fine
y = angles.x; //When active this line constantly resets the y value even if there is input value.
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
Your answer
Follow this Question
Related Questions
camera movements very slow, why!? :) 2 Answers
How can you make a FPS controller camera "fly"? 1 Answer
Mouse Camera Control 0 Answers
Teleporting a camera to a camera 1 Answer
Camera 2D Movement android unity 1 Answer