【问题标题】:How do I flip an enemy moving along patrol points in Unity?如何在 Unity 中翻转沿巡逻点移动的敌人?
【发布时间】:2020-08-09 16:32:01
【问题描述】:

我意识到这是一个常见问题,但我是编码和 Unity 的新手,我所有的“解决方案”都失败了。敌人沿着 2D 地图上的随机巡逻点移动,但它不是对称的,我希望它每次向那个方向移动时都在 x 轴上翻转

这是我的代码:

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

public class PatrolBehavior : StateMachineBehaviour
{
    private GameObject[] patrolPoints;
    public float speed;
    private int randomPoint;
    // 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)
    {
        patrolPoints = GameObject.FindGameObjectsWithTag("patrolPoints");
        randomPoint = Random.Range(0, patrolPoints.Length);

    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.transform.position = Vector2.MoveTowards(animator.transform.position, patrolPoints[randomPoint].transform.position, speed * Time.deltaTime);
        if (Vector2.Distance(animator.transform.position, patrolPoints[randomPoint].transform.position) < 0.1f)
        {
            randomPoint = Random.Range(0, patrolPoints.Length);
        }
    }

    // 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)
    {
        
    }


}

在您询问代码之前,它不是我的。我正在学习 99% 的教程,因此我在解决代码问题时毫无用处。我真的很感激更量身定制的回应,而不是 比“使用 animator.transform.Rotate(0, 180, 0);”因为我不知道如何使用它:(。

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    要在 X 轴上翻转精灵: SpriteRenderer 渲染器 = GetComponent(); renderer.flipX = !renderer.flipX;

    【讨论】:

      【解决方案2】:

      你可以翻页。局部尺度

      LocalScale 代码示例:

      public void FlipSprite() {
          // Get the X Value of the patrolPoint
          float pointXValue = patrolPoints[randomPoint].transform.position.x;
      
          // Get the X Value of the Enemy
          float enemyXValue = gameObject.transform.x
      
          // PatrolPoint is to the Left
          if (pointXValue < enemyXValue) {
              // Flip Sprite on the X Axis
              transform.localScale = new Vector3 (-1f, 1f, 1f);
          }
          // PatrolPoint is to the Right
          else if (pointXValue > enemyXValue) {
              // Flip Sprite back on the X Axis
              transform.localScale = new Vector3 (1f, 1f, 1f);
          }
      }
      

      在您选择了新的 RandomRange 点之后,该段代码需要位于您想要翻转的 GameObject 中,才能访问它的 Transform。每次设置 randomPoint 后都需要调用函数。

      // Getting new patrolPoint
      randomPoint = Random.Range(0, patrolPoints.Length);
      // Fliping Sprite
      FlipSprite();
      

      我不建议您转至 flipX。只有Rendering(Sprite)会受到翻转的影响。其他一切都将保持不变,因此,如果您的 Collider2D 不对称,则可能会导致稍后的 Collisions 出现问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-10
        • 2019-06-07
        • 2014-06-04
        • 1970-01-01
        • 1970-01-01
        • 2023-01-04
        • 1970-01-01
        相关资源
        最近更新 更多