- Home /
Camera rotation around the scene
how to save the zoom value in the following code
// Mouse buttons in the same order as Unity
public enum MouseButton { Left = 0, Right = 1, Middle = 2, None = 3}
Transform target;
float distance = 10.0f;
float xSpeed = 250.0f;
float ySpeed = 120.0f;
float yMinLimit = -20f;
float yMaxLimit = 80f;
private float x = 0.0f;
private float y = 0.0f;
Quaternion originalRotation;
// 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;
}
void LateUpdate () {
if (target) {
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
//Debug.Log(Input.GetAxis("Mouse X") + " " +Input.GetAxis("Mouse Y"));
//Debug.Log("x " + x + " y "+ y+ " "+ transform.position.z);
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0f);
Vector3 position = rotation* new Vector3(0.0f,0.0f,-distance) + target.position; //new Vector3(0.0f, 0.0f, -distance)
transform.rotation = rotation;
transform.position = position;
}
if (Input.GetAxis("Mouse ScrollWheel")>0)
{
float translateZ = Input.GetAxis("Mouse ScrollWheel") * 2;
transform.Translate(0, 0, translateZ);
}
if (Input.GetAxis("Mouse ScrollWheel")<0)
{
float translateZ = Input.GetAxis("Mouse ScrollWheel") * 2;
transform.Translate(0, 0, translateZ);
}
distance = transform.position.z;
}
}
public static float ClampAngle(float angle, float min, float max){
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
// Update is called once per frame
void Update () {
target = GameObject.Find("ScenePivotPoint").transform;
}
I tried to update the distance value during the scene but I get a fancy result, Is there any suggestion??
note - this is usually called an "orbit" and you will fortinately find many free scripts for it, search around!
thank you, I think what I need to study math could you suggest for me a book about Euler angles
Your answer
Follow this Question
Related Questions
How would I make a camera work like the one in ROBLOX? 0 Answers
How to rotate the camera with the player smoothly without the camera snapping back and forth? 2 Answers
[maths q] How to match planes pos in worldspace to skybox on a camera? 0 Answers
How can i mimic the "Frame Selected [F]" camera move? (Zoom Extents, Zoom to Fit) 2 Answers
How to stretch a GUI texture over it's original size? 1 Answer