跟随目标物体移动的功能
程序员文章站
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;
}
}
}