- Home /
Question by
Bageras · Oct 25, 2013 at 03:29 PM ·
camerathird-personanglesperson
3rd person camera doesn't rotate as intended
Hi folks; I'm having a bit of a problem with my 3rd person camera setup. I downloaded a reference file for the camera from the internet which lacked up-and-down movement, so I had to code that in.
However, when I now move up or down and then left or right, the camera moves in unexpected directions. I included a unitypackage file so you can see for yourself: http://www.mediafire.com/download/y9syj4udg1gk5ll/example.unitypackage
And here's a copy of the code used for the camera:
using UnityEngine;
using System.Collections;
public class MouseAimCamera : MonoBehaviour {
public GameObject target;
public float rotateSpeed = 6;
Vector3 offset;
void Start() {
offset = target.transform.position - transform.position;
}
void Update() {
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
target.transform.Rotate(0, horizontal, 0);
float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
target.transform.Rotate(vertical, 0, 0);
float desiredAngleX = target.transform.eulerAngles.y;
float desiredAngleY = target.transform.eulerAngles.x;
Quaternion rotation = Quaternion.Euler(desiredAngleY, desiredAngleX, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt(target.transform);
}
}
If you have any idea how to fix this, please let me know!
Cheers,
Bageras
Comment
Your answer
Follow this Question
Related Questions
Camera Help (WoW-like) 1 Answer
3rd Person Camera Problem 1 Answer
How do I fix this jitter problem? 0 Answers
Camera collision 2 Answers