标签:
using System.Collections;
public class CamFollowPlayer : MonoBehaviour {
public float moveSpeed = 3;
public float rotateSpeed = 3;
private Vector3 offset;
private Transform player;
void Awake()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
offset = transform.position - player.position;
offset = new Vector3 (0, offset.y, offset.z);
}
// Update is called once per frame
void Update ()
{
Vector3 beginPos = player.position + offset;
Vector3 endPos = player.position + offset.magnitude * Vector3.up;
Vector3 pos1 = Vector3.Lerp (beginPos, endPos, 0.25f);
Vector3 pos2 = Vector3.Lerp (beginPos, endPos, 0.5f);
Vector3 pos3 = Vector3.Lerp (beginPos, endPos, 0.75f);
Vector3[] posArray = new Vector3[]{beginPos, pos1, pos2, pos3, endPos};
Vector3 targetPos = posArray [0];
for (int i = 0; i < 5; i++)
{
RaycastHit hitInfo; //存储射线碰撞检测信息
if(Physics.Raycast(posArray[i], player.position - posArray[i], out hitInfo)) //起始位置 ,方向 ,输出信息 返回是否有射线打到位置
{
if(hitInfo.collider.tag != "Player")
{
continue;
}
else
{
targetPos = posArray[i];
break;
}
}
else
{
targetPos = posArray[0];
break;
}
}
this.transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * moveSpeed);
Quaternion nowRotation = transform.rotation;
transform.LookAt (player.position);
transform.rotation = Quaternion.Lerp (nowRotation, transform.rotation, Time.deltaTime * rotateSpeed);
}
}
摄像机跟随物体,修复物体遮挡
标签:
原文地址:http://www.cnblogs.com/DragonXin/p/4810728.html