Rotate Around a character World X axis - weird issue
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.
Answer by Egalestrenge · Feb 25, 2016 at 07:33 PM
Ok, never mind, now I've come up with the explanation: of course, if the camera rotates around the Y-axis, then the rotation around the X-axis will be afected, since the X-axis is allways pointing at the world X direction, and hence the rotation will change depending upon the position of the camera (i.e, the rotation plane will be more or less distanciated from the character).
Sorry for the stupid question lol.
Out of curiosity, how did you end up solving your camera issue? I'm having the same problem and am a bit stumped.
Thanks!
Your answer
Follow this Question
Related Questions
Rotate an object around one of his childs in the self space (not the world space) 2 Answers
Rotate object arround other object, but always facing the camera 1 Answer
Rotatin an object around another object 0 Answers
Rotate object with mouse 0 Answers
Make GameObject rotate around another around random axis but with fixed distance 0 Answers