Unity3d 统一计算动态停车距离
对于控制NammesAgent,在到达我使用的OnAnimatorMove()目标后停止。但当它到达目的地时,运行动画不会停止 我使用BlendTree控制动画。这是我的代码:Unity3d 统一计算动态停车距离,unity3d,Unity3d,对于控制NammesAgent,在到达我使用的OnAnimatorMove()目标后停止。但当它到达目的地时,运行动画不会停止 我使用BlendTree控制动画。这是我的代码: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; // private vriables private Animator ani
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;
}
注意:我在NavMesh组件中将stoppingDistance设置为0
所以我需要计算玩家和目标之间的停止距离。代码中的一些小问题:
- 您将
速度设置为0代码>但除了navAgent.speed之外,我看不到任何地方定义了速度-确保使用了正确/预期的变量李>
基本上是Vector3.Distance(transform.position,target.position)>navAgent.stoppingDistance
-将stoppingDistance替换为Distance>0
或其他一些小值。距离几乎永远不会完全为零,因此(几乎)总是>00.2f
- 您可以将动画的速度设置为代理速度,这样,如果播放器不移动,动画将“运行”,但实际上不会移动。它也会随着加速度自动伸缩。也许你的混合树也会更好用
- 你为什么把停车距离设为0
- 问题出在这部分,我将其删除
private void OnAnimatorMove()
{
navAgent.speed = (anim.deltaPosition / Time.deltaTime).magnitude;
}
我还将stoppingDistance设置为none零。我将编辑代码放在这里,不幸地删除了速度变量。我希望我的角色停在目标位置,我认为应该将停站距离设置为0。问题是当导航代理到达目的地时,速度大于1。我修正了它。谢谢。到底是什么问题?为了更好地解释,我把答案贴在了网上。
private void OnAnimatorMove()
{
navAgent.speed = (anim.deltaPosition / Time.deltaTime).magnitude;
}