I’m trying to rotate the camera around a box, like when the box moves on the X angle, the camera will have a position on the X angle, following the box, but If the player press “D” the box starts to move on the Z angle and the camera needs to rotate around that box to have a position at the back of the box.
My script(as you can see,I tried to rotate the camera manually, but I failed):
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject cube;
public float rotateSpeed = 5;
Vector3 offset;
// Use this for initialization
void Start () {
offset = cube.transform.position - transform.position;
}
// Update is called once per frame
void Update () {
//if (CharachterControll.movingDirect == 1) {
//transform.position = new Vector3(cube.transform.position.x - 12, cube.transform.position.y + 8, transform.position.z);
//}
//else if (CharachterControll.movingDirect == 2)
// {
//transform.position = new Vector3(cube.transform.position.x, cube.transform.position.y + 8, transform.position.z - 12);
//}
}
void LateUpdate()
{
if (CharachterControll.movingDirect == 1)
{
//float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
//cube.transform.Rotate(0, horizontal, 0);
float desiredAngle = cube.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
transform.position = cube.transform.position - (rotation * offset);
}
else if (CharachterControll.movingDirect == 2)
{
float desiredAngle = cube.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
transform.position = cube.transform.position - (rotation * offset);
}
transform.LookAt(cube.transform);
}
}
(Sorry for my english)