- Home /
Question by
KRSBX · Aug 05, 2018 at 04:44 PM ·
camera-movementcamera-looksmoothfollow
How to make an camera offsets in Unity Smooth Follow Standard Assets?
So, I tried to rip off the script from the standard assets and edited like this: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class camFollow : MonoBehaviour {
// The target we are following
[SerializeField]
private Transform target;
[SerializeField]
private Transform player;
// The distance in the x-z plane to the target
[SerializeField]
private float rotationDamping;
[SerializeField]
private float heightDamping;
public Vector3 offset = new Vector3(2.5f, 0f, -3f);
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + globVar.camHeight;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * globVar.camDistance;
// Set the height of the camera
transform.position = new Vector3 (transform.position.x+3, currentHeight, transform.position.z);
// Always look at the target
transform.LookAt (target);
}
}
but the effect of it is the camera goes to the right of the player object and the rotation became weird. Did anybody know how to fix it?
Comment