Hey Guys! I am making a script for a little moba camera. and I can’t really visualize how to do a rotation lock. for example instead of always following the back of a player, it is always faces north(for example) but still follows the player. I have done this script so far…
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control")]
public class Follow : MonoBehaviour
{
public Transform target;
public float distance = 10.0f;
public float height = 5.0f;
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
public bool FreeCam = false;
void LateUpdate() {
if(!FreeCam){
if (!target)
return;
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
transform.LookAt(target);
}
else
{
if (Input.GetKey(KeyCode.W))
{
Vector3 posz = transform.position;
posz.z = posz.z + 1;
transform.position = posz;
}
if (Input.GetKey(KeyCode.S))
{
Vector3 posz = transform.position;
posz.z = posz.z - 1;
transform.position = posz;
}
if (Input.GetKey(KeyCode.A))
{
Vector3 posx = transform.position;
posx.x = posx.x - 1;
transform.position = posx;
}
if (Input.GetKey(KeyCode.D))
{
Vector3 posx = transform.position;
posx.x = posx.x + 1;
transform.position = posx;
}
}
}
void Update()
{
if(Input.GetKeyUp(KeyCode.F))
{
if (FreeCam)
{
Debug.Log("FreeCam false");
FreeCam = false;
}else
{
Debug.Log("FreeCam true");
FreeCam = true;
}
}
}
}
but this code only follows the back of the player.