I made a simple script for camera follow and camera rotate. I tried then to make the rotation smooth, but when adding that i simply cannot get the camera to follow the player object (a sphere) and to rotate smoothly.
Can someone tell me what im doing wrong and how i can make the code better for it to work?
Thanks in advance!
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
public Vector3 offset;
public Transform target;
public int i = 1;
private float smoothSpeed = 0.125f;
void Start ()
{
}
void Update()
{
// object follow code
transform.position = transform.position + offset;
// camera rotation code
transform.position = Vector3.Lerp(transform.position, offset, smoothSpeed);
CameraVectors();
}
void LateUpdate ()
{
transform.LookAt(player.transform);
if (Input.GetKeyDown(KeyCode.E))
{
if (i == 4)
{
i = 1;
}
else
{
i++;
}
}
}
void CameraVectors ()
{
switch (i)
{
case 1:
offset = new Vector3(0, 5, -5);
break;
case 2:
offset = new Vector3(5, 5, 0);
break;
case 3:
offset = new Vector3(0, 5, 5);
break;
case 4:
offset = new Vector3(-5, 5, 0);
break;
default:
offset = new Vector3(0, 5, -5);
break;
}
}
}