C# 如何修复我的2D敌人角色在靠近它时发出警告时滑动而不是停止';s范围

C# 如何修复我的2D敌人角色在靠近它时发出警告时滑动而不是停止';s范围,c#,unity3d,2d-games,C#,Unity3d,2d Games,我对C#还是新手,我想知道如何解决这个问题。当敌人的骨架到达角色的范围时,它似乎会滑动。我试图找到解决办法,但没有找到 ------------------------------敌书-------------------------------- private Rigidbody2D myBody; [Header("Movement")] public float moveSpeed; private float minX, maxX; public float distance; pu

我对C#还是新手,我想知道如何解决这个问题。当敌人的骨架到达角色的范围时,它似乎会滑动。我试图找到解决办法,但没有找到

------------------------------敌书--------------------------------

private Rigidbody2D myBody;

[Header("Movement")]
public float moveSpeed;
private float minX, maxX;
public float distance;
public int direction;

private bool patrol, detect;

private Transform playerPos;
private Animator anim;

[Header("Attack")]
public Transform attackPos;
public float attackRange;
public LayerMask playerLayer;
public int damage;
清醒 更新
void Update()
{
if(矢量3.距离(transform.position,playerPos.position)0)
{
transform.localScale=newvector2(1f,transform.localScale.y);
动画挫折(“攻击”,错误);
}
否则如果
(myBody.velocity.x<0)transform.localScale=新向量2(-1f,transform.localScale.y);
如果(巡逻)
{
检测=假;
开关(方向)
{
案例1:
if(transform.position.x>minX)
myBody.velocity=newvector2(-moveSpeed,myBody.velocity.y);
其他的
方向=1;
打破
案例1:
if(transform.position.x=1.0f)
{
如果(!检测)
{
检测=真;
动画设置触发(“检测”);
myBody.velocity=新向量2(0,myBody.velocity.y);
}
if(anim.GetCurrentAnimatorStateInfo(0.IsName(“detect”))返回;
Vector3 playerDir=(playerPos.position-transform.position);
如果(playerDir.x>0)
myBody.velocity=newvector2(移动速度+0.4f,myBody.velocity.y);
其他的
myBody.velocity=新矢量2(-moveSpeed+0.4f),myBody.velocity.y;
}

else if(Vector2.Distance(playerPos.position,transform.position)不确定,但我猜
if(anim.GetCurrentAnimatorStateInfo(0.IsName(“detect”)
返回false,其余代码将被执行。
        void Awake()
        {
            anim = GetComponent<Animator>();
            playerPos = GameObject.Find("George").transform;
            myBody = GetComponent<Rigidbody2D>();
        }
        private void Start()
        {
            maxX = transform.position.x + (distance);
            minX = maxX - distance;

            //if (Random.value > 0.5) direction = 1;
            //else direction = -1;
        }
        void Update()
        {
            if (Vector3.Distance(transform.position, playerPos.position) <= 4.0f) patrol = false;
            else patrol = true;
        }
        private void FixedUpdate()
        {
            if (anim.GetBool("Death"))
            {
                myBody.velocity = Vector2.zero;
                GetComponent<Collider2D>().enabled = false;
                myBody.isKinematic = true;
                anim.SetBool("Attack", false);
                return;
            }


            if (myBody.velocity.x > 0)
            {
                transform.localScale = new Vector2(1f, transform.localScale.y);
                anim.SetBool("Attack", false);
            }
            else if
                (myBody.velocity.x < 0) transform.localScale = new Vector2(-1f, transform.localScale.y);


            if (patrol)
            {
                detect = false;
                switch (direction)
                {
                    case -1:
                        if (transform.position.x > minX)
                            myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
                        else
                            direction = 1;
                        break;
                    case 1:
                        if (transform.position.x < maxX)
                            myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
                        else
                            direction = -1;
                        break;
                }
            }
            else
            {
                if (Vector2.Distance(playerPos.position, transform.position) >= 1.0f)
                {
                    if (!detect)
                    {
                        detect = true;
                        anim.SetTrigger("Detect");
                        myBody.velocity = new Vector2(0, myBody.velocity.y);
                    }
                    if (anim.GetCurrentAnimatorStateInfo(0).IsName("detect")) return;

                    Vector3 playerDir = (playerPos.position - transform.position).normalized;

                    if (playerDir.x > 0)
                        myBody.velocity = new Vector2(moveSpeed + 0.4f, myBody.velocity.y);
                    else
                        myBody.velocity = new Vector2(-(moveSpeed + 0.4f), myBody.velocity.y);
                }
                else if (Vector2.Distance(playerPos.position, transform.position) <= 1.0f)
                {
                    myBody.velocity = new Vector2(0, myBody.velocity.y);
                    anim.SetBool("Attack", true);
                }
            }
        }