首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一计算动态stoppingDistance

统一计算动态stoppingDistance
EN

Stack Overflow用户
提问于 2018-09-11 23:39:33
回答 2查看 520关注 0票数 1

对于控制NammeshAgent和到达目的地后停止,我使用了OnAnimatorMove()。但是当它到达目的地时,运行动画不会停止。

我使用BlendTree制作控件动画。所以这是我的代码:

代码语言:javascript
复制
    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。我在下面的打印中看到了这一点。

代码语言:javascript
复制
Vector3.Distance(transform.position, target.position)

注意:我在NavMesh组件中将stoppingDistance设置为0。

所以我需要计算玩家和目标之间的停止距离。

EN

回答 2

Stack Overflow用户

发布于 2018-09-11 23:56:44

代码中的一些小问题:

你设置了speed = 0;,但我没有看到任何地方定义速度,除了navAgent.speed -确保你使用正确的/预期的速度基本上是distance > 0 -将stoppingDistance替换为0.2f或其他较小的值。距离几乎永远不会恰好为零,因此(几乎)总是>0。

一些想法:

  • 您可以将动画的速度设置为代理速度,这样,如果播放器不移动,动画将“运行”,但不会实际移动。此外,它还会自动随加速进行扩展。也许你的blendTrees也会工作得更好。
  • 为什么你把停车距离设为0?
票数 1
EN

Stack Overflow用户

发布于 2018-09-12 05:34:37

这个部分有问题,我把它去掉了。

代码语言:javascript
复制
private void OnAnimatorMove()
{
    navAgent.speed = (anim.deltaPosition / Time.deltaTime).magnitude;
}

我还将stoppingDistance设置为none和0。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52279606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档