对于控制NammeshAgent和到达目的地后停止,我使用了OnAnimatorMove()。但是当它到达目的地时,运行动画不会停止。
我使用BlendTree制作控件动画。所以这是我的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
// private vriables
private Animator anim;
private NavMeshAgent navAgent;
public Transform target;
private float speed;
void Start()
{
anim = GetComponent<Animator>();
navAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
MoveToDestination();
}
private void MoveToDestination()
{
// Move to Target
if (Vector3.Distance(transform.position, target.position) > navAgent.stoppingDistance)
{
speed = 1f;
navAgent.SetDestination(target.position);
}
else
{
speed = 0;
}
anim.SetFloat("Speed", speed, 0.1f, Time.deltaTime);
}
private void OnAnimatorMove()
{
navAgent.speed = (anim.deltaPosition / Time.deltaTime).magnitude;
}注意:当NavmeshAgent到达目的地时,NavmeshAgent组件中的速度值大于1。我在下面的打印中看到了这一点。
Vector3.Distance(transform.position, target.position)注意:我在NavMesh组件中将stoppingDistance设置为0。
所以我需要计算玩家和目标之间的停止距离。
发布于 2018-09-11 23:56:44
代码中的一些小问题:
你设置了speed = 0;,但我没有看到任何地方定义速度,除了navAgent.speed -确保你使用正确的/预期的速度基本上是distance > 0 -将stoppingDistance替换为0.2f或其他较小的值。距离几乎永远不会恰好为零,因此(几乎)总是>0。
一些想法:
发布于 2018-09-12 05:34:37
这个部分有问题,我把它去掉了。
private void OnAnimatorMove()
{
navAgent.speed = (anim.deltaPosition / Time.deltaTime).magnitude;
}我还将stoppingDistance设置为none和0。
https://stackoverflow.com/questions/52279606
复制相似问题