- Home /
Rotating camera around object up/down problem
Hey everyone! Ive been into unity3d for a few days now, ive browsed through several Q&A posts and havent found solution to this yet. What im trying to do is make the main camera orbit around player when pressing WASD keys. A and D are working perfectly fine, but W and S are having problems, the camera starts to rotate very weirdly when pressing them. Its hard to describe the movement that camera makes, but i think you can find the issue in my code.
#pragma strict
var Target : Transform;
var CameraSpeed = 0.1;
var DistanceToPlayer : float;
var xRot : float;
var MaxX = 55;
var MinX = 5;
private var point;
function Start () {
}
function Update () {
xRot = transform.eulerAngles.x;
point = Target.position;
if(Input.GetKey(KeyCode.A)){
transform.RotateAround(point,new Vector3(0f,1f,0f), CameraSpeed);
transform.LookAt(Target);
}
if(Input.GetKey(KeyCode.D)){
transform.RotateAround(point,new Vector3(0f,-1f,0f), CameraSpeed);
transform.LookAt(Target);
}
if(Input.GetKey(KeyCode.W) && xRot < MaxX){
transform.RotateAround(point,new Vector3(1f,0f,0f), CameraSpeed);
transform.LookAt(Target);
}
if(Input.GetKey(KeyCode.S) && xRot > MinX){
transform.RotateAround(point,new Vector3(-1f,0f,0f), CameraSpeed);
transform.LookAt(Target);
}
}
Comment
I've got the same problem. Z component should be intact when I rotate the camera up and down, but cannot find the right axis.
Did you ever figure it out? Having the same issue.