unity3d 如何改变AI敌人转弯速度

mitkmikd  于 2023-02-16  发布在  其他
关注(0)|答案(2)|浏览(185)

我有敌人的AI和巡逻点,当他到达一个点,他开始转向下一个点,并向它移动。所以它转向太慢,如何加快这个转向?我用transform.lookAt(),但它立即旋转。

[SerializeField] private NavMeshAgent _agent;
[SerializeField] private List<Transform> _targets;
private float _patrolDistance;
private int index;

private void FixedUpdate()
{
    _patrolDistance = Vector3.Distance(_agent.transform.position, _agent.pathEndPosition);

    if (_patrolDistance < 0.1f)
    {
        RandomTarget();
        transform.LookAt(_targets[index]);
    }
    MovePatrolPoint();
}

private void RandomTarget()
{
    index = Random.Range(0, _targets.Count);
}

private void MovePatrolPoint()
{
    _agent.SetDestination(_targets[index].position);
}

}

z8dt9xmd

z8dt9xmd1#

您可以使用Quaternion.SlerpQuaternion.LookRotation等替代方法来实现更平滑的旋转

bjp0bcyl

bjp0bcyl2#

我会用一个slerp来做这样的事情,用一个变量乘以delta时间来控制速度。

[SerializeField] private NavMeshAgent _agent;
    [SerializeField] private List<Transform> _targets;
    [SerializeField] private float Speed = 1;
    private float _patrolDistance;
    private Transform TargetTransform;
    private Vector3 Target => TargetTransform.position;
    private void Update()
    {
        CheckTargetDistance();
    }

    private void CheckTargetDistance()
    {
        _patrolDistance = Vector3.Distance(_agent.transform.position, _agent.pathEndPosition);

        if (_patrolDistance < 0.1f)
        {
            RandomTarget();
        }
    }

    private void FixedUpdate()
    {
        LookAtTarget();
        MovePatrolPoint();
    }

    private void RandomTarget()
    {
        int index = Random.Range(0, _targets.Count);
        TargetTransform = _targets[index];
    }

    private void MovePatrolPoint()
    {
        _agent.SetDestination(Target);
    }
    private void LookAtTarget()
    {
        Quaternion lookRotation = Quaternion.LookRotation(Target - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Speed * Time.deltaTime);
    }

相关问题