Player的跟踪狂 -- Camera
P.S.很多游戏里的Player都会设置的被跟踪,是人性的扭曲,还是XXX,正在解密。
第三人称视角
- camera紧跟player背后(角度随player改变)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowTarget : MonoBehaviour {
private Transform player;
private Vector3 offset;//偏移值
private float smoothing = 3.0f;//平滑度
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player").transform;
offset = transform.position - player.position;
}
// Update is called once per frame
void LateUpdate () {
Vector3 targetPosition = player.position + player.TransformDirection(offset);//局部坐标,跟随背后
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothing);
transform.LookAt(player);
}
}
效果如下:(一直保持这个视角)
- camera保持一个角度做俯视(像被上帝注视,颤抖吧player!)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowTarget : MonoBehaviour {
private Transform player;
private Vector3 offset;
private float smoothing = 3.0f;//平滑度
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player").transform;
offset = transform.position - player.position;
}
// Update is called once per frame
void LateUpdate () {
Vector3 targetPosition = player.position + offset;//世界坐标,上帝视角
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothing);
transform.LookAt(player);
}
}
效果如下:(可以看到身前)