-
这个我和老师您做的方法是一样的,我觉得是这个方法问题是因为我学过3d软件中的注视约束在
经过最上方时,也会有近乎相同的情况,但是在这里也不知道是什么情况,下面是我照着老师写的代码,
不知道具体要复制那些重要的所以全复制过来了,麻烦老师了。
public class FollowPlayer : MonoBehaviour
{
private Transform player;
public Vector3 offset;
public float posSpeed = 0.25f;
public float rotateSpeed = 0.25f;// Use this for initialization
void Start ()
{
player = GameObject.FindWithTag(Tags.player).transform;
offset = transform.position - player.position;
offset=new Vector3(0,offset.y,offset.z);
}// Update is called once per frame
void Update ()
{
//transform.position = player.position + offset;
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 hit;
if (Physics.Raycast(posArray[i],player.position-posArray[i],out hit))
{
if (hit.collider.tag!=Tags.player)
{
continue;
}
else
{
targetPos = posArray[i];
break;
}
}
else
{
targetPos = posArray[i];
break;
}
}
//transform.position = targetPos;
Quaternion nowRotate = transform.rotation;
transform.LookAt(player.position,Vector3.up);
transform.position = Vector3.Lerp(transform.position, targetPos, posSpeed * Time.deltaTime);
Quaternion.Lerp(nowRotate, transform.rotation, rotateSpeed);