using UnityEngine;标签:unity3d,Vector3,C#,float,private,相机,wantedPosition,currentY,currentX From: https://blog.51cto.com/u_8378185/5990730
using System.Collections;
public class CameraFollow : MonoBehaviour {
public Transform target;
private Vector3 wantedPosition;
private float currentX;
private float currentY;
private float currentZ;
private float xVelocity = 0.0F;
private float yVelocity = 0.0F;
private float zVelocity = 0.0f;
private float distanceSnapTime = 0.1f;
// Update is called once per frame
void Update () {
Vector3 targetPos = target.position;
wantedPosition.x = targetPos.x;
wantedPosition.z = targetPos.z - 5f;//Vector3.forward*distance;
wantedPosition.y = targetPos.y -2f;// + heightAbovePlayer;
currentX = Mathf.SmoothDamp(currentX, wantedPosition.x, ref xVelocity, distanceSnapTime);
currentY = Mathf.SmoothDamp(currentY, wantedPosition.y, ref yVelocity, distanceSnapTime);
currentZ = Mathf.SmoothDamp(currentZ, wantedPosition.z, ref zVelocity, 0.5f);
transform.position = new Vector3(currentX,currentY,currentZ);
transform.LookAt(transform.position + new Vector3(0f,0.95f,1));
}
}