Hello, I’m trying to get the main camera to rotate around the main character in two possible axis: Y and X. Both of them are archieved by the RotateAround() method:
using UnityEngine;
using System.Collections;
public class world_camera : MonoBehaviour {
GameObject MC;
Vector3 MCInitPos, MCFinPos, cameraMCPos;
public float turnSpeed, thetaAngle;
// Use this for initialization
void Start () {
MC = GameObject.FindWithTag("Player");
MCInitPos = MC.transform.position;
turnSpeed = 20.0F;
gameObject.transform.LookAt(MC.transform.position);
}
// Update is called once per frame
void Update () {
MC = GameObject.FindWithTag("Player");
MCFinPos = MC.transform.position;
cameraMCPos = gameObject.transform.position - MCFinPos;
thetaAngle = Mathf.Acos(cameraMCPos.y/cameraMCPos.magnitude);
if(Input.GetKey(KeyCode.LeftArrow)){
gameObject.transform.RotateAround(MC.transform.position,Vector3.up,turnSpeed*Time.deltaTime);
}
else if(Input.GetKey(KeyCode.RightArrow)){
gameObject.transform.RotateAround(MC.transform.position,Vector3.up,-turnSpeed*Time.deltaTime);
}
else if(Input.GetKey(KeyCode.UpArrow)){
if(thetaAngle>(0.0F+0.02F)){
gameObject.transform.RotateAround(MC.transform.position,Vector3.right,turnSpeed*Time.deltaTime);}
}
else if(Input.GetKey(KeyCode.DownArrow)){
if(thetaAngle<(Mathf.PI/2-0.02F)){
gameObject.transform.RotateAround(MC.transform.position,Vector3.right,-turnSpeed*Time.deltaTime);}
}
else if(Input.GetKey(KeyCode.Keypad0)){
gameObject.transform.Translate(Vector3.forward*turnSpeed*Time.deltaTime);
}
else if(Input.GetKey(KeyCode.KeypadPeriod)){
gameObject.transform.Translate(-Vector3.forward*turnSpeed*Time.deltaTime);
}
gameObject.transform.LookAt(MC.transform.position);
gameObject.transform.position+=new Vector3((MCFinPos-MCInitPos).x,0,(MCFinPos-MCInitPos).z);
MCInitPos = MC.transform.position;
The thing is: the Y-axis rotation works fine: it rotates around the main character, but the X-axis rotation is weird, since it performs a rotation around the X-axis, not OVER the main character, but a bit displaced.
It is just like the rotation is performed in another plane that doesn’t cross the main character, as it should be if I want an X-rotation over it.
Have you got any thoughts about why is this?
Thank you very much in advance.