- Home /
How to move camera on an angle
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)
I don't understand what you are asking. Often language gets in the way of understanding rotation problems. A drawing might help.
Answer by robertbu · Oct 12, 2014 at 05:30 PM
I'm out of time for a detailed answer. It can be done with a single game object, but a simple solution can be done with two objects:
Create an empty game object and place it at the center of the box. The empty game object's initial rotation must match the box.
Make the camera a child of the empty game object at the right distance and looking at the box.
Add a line of code in LateUpdate() that has the empty game object tracking the box (it is not a child, simply following the position of the box).
Have the empty game object rotate to look at the direction of movement.
Slerp or Lerp the rotation as necessary.
please can someone explain the last 3 lines of instructions for a noob please