对象(无)在Unity3D动画状态机器中移动

发布于 2025-02-07 02:37:52 字数 1716 浏览 3 评论 0原文

我正在尝试学习如何实现动画状态机&我似乎在某个地方出错了。

当我尝试使敌方物体遵循并在接近时触发“攻击”时。

触发攻击作品,但是敌人的物体拒绝跟随甚至为此移动。

我不确定我是否在下面的代码中犯了一个错误,还是在其他地方(例如动画本身)犯了错误。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyRun : StateMachineBehaviour
{

    public float speed = 2.5f;
    public float attackRange = 3f;

    Transform player;
    Rigidbody rb;
    //Boss boss;

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        rb = animator.GetComponent<Rigidbody>();
        //boss = animator.GetComponent<enemy>();

    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        //boss.LookAtPlayer();

        Vector3 target = new Vector3(player.position.x, player.position.y, player.position.z);
        Vector3 newPos = Vector3.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime);
        rb.MovePosition(newPos);
    
        if (Vector3.Distance(player.position, rb.position) <= attackRange)
        {
            animator.SetTrigger("attack");
        }
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.ResetTrigger("attack");
    }
}

I am trying to learn how to implement animation state machines & I seem to be going wrong somewhere.

When I try and make an enemy object follow and trigger an 'attack' when in proximity.

Triggering an attack works, however the enemy object refuses to follow or even move for that matter.

I'm not sure if I have made a mistake in the code below or there is somewhere else, like the animations themselves.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyRun : StateMachineBehaviour
{

    public float speed = 2.5f;
    public float attackRange = 3f;

    Transform player;
    Rigidbody rb;
    //Boss boss;

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        rb = animator.GetComponent<Rigidbody>();
        //boss = animator.GetComponent<enemy>();

    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        //boss.LookAtPlayer();

        Vector3 target = new Vector3(player.position.x, player.position.y, player.position.z);
        Vector3 newPos = Vector3.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime);
        rb.MovePosition(newPos);
    
        if (Vector3.Distance(player.position, rb.position) <= attackRange)
        {
            animator.SetTrigger("attack");
        }
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.ResetTrigger("attack");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2025-02-14 02:37:52

有一个非常不错的AI解决方案,可以关注对象 - https://www.youtube.com/watch.com/watch.com/watch.com/watch.com/watch ?v = mx9m0ier1m0

努力使动画与一方合作,但希望有可能

There's a really nice AI solution for objects to follow - https://www.youtube.com/watch?v=Mx9M0ieR1M0

Struggling to get animations to work along side but hopefully it's possible

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文