对象(无)在Unity3D动画状态机器中移动
我正在尝试学习如何实现动画状态机&我似乎在某个地方出错了。
当我尝试使敌方物体遵循并在接近时触发“攻击”时。
触发攻击作品,但是敌人的物体拒绝跟随甚至为此移动。
我不确定我是否在下面的代码中犯了一个错误,还是在其他地方(例如动画本身)犯了错误。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个非常不错的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