Question by
Dalek.skaro · Oct 11, 2015 at 10:00 PM ·
c#camerarotationscripting beginner
How do i limit the rotation of a game object in relation to a free following camera ?
hello, i'm trying to make a following camera script, but i don't know how to limit the maximum rotation to the gameobject it is following.
to be more clear here is an example :
no rotation in relation to camera ( ideal state ) :
180°-ish of rotation in relation to camera (very not ideal state) :
to be clear i know about making the camera a child of the gameobject, but that is too fixed, i want a little response to turning and ascending/descending, of couse with a limit of rotation (someting like -75° to 75° in relation to the y axis of the camera) .
here is the following camera script :
public class CameraScript : MonoBehaviour {
Transform playerTrs;
Quaternion targetLook;
Vector3 targetMove;
public float smoothLook = 0.9F;
public float smoothMove = 0.5F;
Vector3 smoothMoveV;
public float camDist = 17;
public float camHeight = 4;
public float lookAngle = 5;
void Start () {
playerTrs = GameObject.FindWithTag ("Player").transform;
}
void Update () {
targetMove = playerTrs.position + (playerTrs.rotation * new Vector3 (0, camHeight, camDist));
//transform.position = Vector3.Lerp (transform.position,targetMove,smoothMove * Time.deltaTime);
transform.position = Vector3.SmoothDamp (transform.position, targetMove, ref smoothMoveV, smoothMove);
targetLook = Quaternion.LookRotation (playerTrs.position - transform.position + new Vector3 (0,lookAngle,0));
transform.rotation = Quaternion.Lerp (transform.rotation, targetLook, smoothLook * Time.deltaTime);
//transform.LookAt (playerTrs);
}
}
shipaa.png
(48.1 kB)
shipbb.png
(68.8 kB)
Comment