欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

跟随目标物体移动的功能

程序员文章站 2022-07-14 23:08:37
...
public class FollowTarget : MonoBehaviour
{
    public enum CommonPositionType { OnlyCommonPosition, RotateAroundTargetAxis_Y }

    public CommonPositionType commonPositionType = CommonPositionType.OnlyCommonPosition;

    public Transform followTarget;

    public Vector3 offestVector;

    public bool isFollowing = false;

    public float speed = 1;

    void Update()
    {
        Follow();
    }

    private void Follow()
    {
        if (!isFollowing || followTarget == null) return;

        switch (commonPositionType)
        {
            case CommonPositionType.OnlyCommonPosition:
                Vector3 endPos = followTarget.position + offestVector;
                if (speed < 20)
                    this.transform.position = Vector3.Lerp(this.transform.position, endPos, Time.deltaTime * speed);
                else
                    this.transform.position = endPos;
                break;
            case CommonPositionType.RotateAroundTargetAxis_Y:
                this.transform.position = Quaternion.AngleAxis(followTarget.eulerAngles.y, Vector3.up) * offestVector + followTarget.position;
                break;
            default:
                break;
        }
    }
}

 

相关标签: Follow