- Home /
Question by
Will21 · Apr 10, 2017 at 08:17 PM ·
camerascripting problemcamera-movementcamera followsmoothfollow
Smooth camera script looking at left hand side of character.
The smooth follow camera script is working and following correctly. However, it is looking at the target from the left-hand side rather than the back of the target. Any help or workarounds would be appreciated. Thanks using UnityEngine; using System.Collections;
public class R4Wing_SmoothFollowCam : MonoBehaviour {
public Transform target;
public float distance;
public float height;
public float heightDamping;
public float rotationDamping;
void LateUpdate () {
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);
}
}
Comment
Honestly I've always found this method to provide inconsistent results. However, I would venture to guess the problem lies here transform.position -= currentRotation * Vector3.forward * distance;
because you aren't referencing an actual relative forward position, Vector3.forward is only shorthand for the default 0,0,1. Try transform.forward.