简体   繁体   English

统一 2d 代码错误翻转敌人精灵 (IA) 代码?

[英]error with code for unity 2d flip a enemy sprite (IA) code?

i want to do a enemy IA chasing my character, i can do that, but when i want to code a script for the enemy sprite flip (Its a cube with 2 eyes) with a simple flip technique, the script doesnt work.我想做一个敌人 IA 追逐我的角色,我可以做到,但是当我想使用简单的翻转技术为敌人的精灵翻转(它是一个有 2 只眼睛的立方体)编写脚本时,该脚本不起作用。

this is a little part of my code because a lot of then is for IA movement.这是我的代码的一小部分,因为很多都是为了 IA 运动。

plz help请帮忙

if (facingRight == false && moveInput > 0)
    {
        Flip();
    }
    else if (facingRight == true && moveInput < 0)
    {
        Flip();
    }
}

private void Flip()
{
    facingRight = !facingRight;
    transform.Rotate(0f, 180f, 0f);
}

Rotating your sprite is going to "flip" it, its going to well... rotate it.旋转你的精灵会“翻转”它,它会很好......旋转它。

The old fashioned way to flip a sprite was to set a negative scale, but that was a little counter intuitive so Unity helped us out by Adding flipX .翻转精灵的老式方法是设置负比例,但这有点违反直觉,因此 Unity 通过添加flipX 来帮助我们。 I have updated your code below.我已经在下面更新了您的代码。

if (facingRight == false && moveInput > 0)
    {
        Flip();
    }
    else if (facingRight == true && moveInput < 0)
    {
        Flip();
    }
}

private void Flip()
{
    facingRight = !facingRight;
    GetComponet<SpriteRenderer>().flipX = facingRight;
}

Best of luck!祝你好运! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM