Unity 2D:如何使巡逻AI转身?

时间:2019-01-12 11:49:12

标签: c# unity3d

我正在Unity中制作2D平台游戏,并使用教程视频中的代码使巡逻的敌人成为了敌人。敌人基本上是随机移动到场景中的不同位置。但是我怎样才能使精灵旋转呢?

到目前为止,这是我的代码。我尝试了不同的方法,但是没有得到预期的行为。

public float speed = 10f;
private float waitTime;
public float startWaitTime = 1f;

public Transform[] moveSpots;
private int randomSpot;

private bool moving = true;
private bool m_FacingRight = true;

void Start()
{
    waitTime = startWaitTime;
    randomSpot = Random.Range(0, moveSpots.Length);
}

void Update()
{
    transform.position = Vector2.MoveTowards(transform.position, moveSpots[randomSpot].position, speed * Time.deltaTime);

    if (Vector2.Distance(transform.position, moveSpots[randomSpot].position) < 0.2f)
    {

        //Debug.Log(m_FacingRight);

        // if (moving.x > 0 && !m_FacingRight)
        // {
        //     Debug.Log("moving right");
        //     Flip();
        // }
        // else if (moving.x < 0 && m_FacingRight)
        // {
        //     Debug.Log("moving left");
        //     Flip();
        // }

        if (waitTime <= 0)
        {
            //moving = true;
            randomSpot = Random.Range(0, moveSpots.Length);
            waitTime = startWaitTime;
        }
        else
        {
            //moving = false;
            waitTime -= Time.deltaTime;
        }
    }
}

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

************************************编辑************ ****************************

我最终是为了敌人的剧本动作

private bool facingRight = true;
    private Rigidbody2D rigidBody2D;
    private SpriteRenderer spriteRenderer;

    public Vector2 speed = new Vector2(10, 0);
    public Vector2 direction = new Vector2(1, 0);

    private void Awake()
    {
        rigidBody2D = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("Wall") || collision.collider.CompareTag("Player"))
        {
            direction = Vector2.Scale(direction, new Vector2(-1, 0));
        }

        if (!collision.collider.CompareTag("Ground"))
        {
            if (!facingRight)
            {
                Flip();
            }
            else if (facingRight)
            {
                Flip();
            }
        }
    }

    void FixedUpdate()
    {
        Vector2 movement = new Vector2(speed.x * direction.x, 0);
        movement *= Time.deltaTime;
        transform.Translate(movement);
    }

    private void Flip()
    {
        facingRight = !facingRight;
        if (!facingRight)
        {
            spriteRenderer.flipX = true;
        }
        else if (facingRight)
        {
            spriteRenderer.flipX = false;
        }
    }

2 个答案:

答案 0 :(得分:0)

在sidescroller中,一种简单的方法是使用SpriteRenderer.flipX

例如,如果要在速度为负时翻转X:

var sr = GetComponent<SpriteRenderer>();
sr.flipX = velocity.x < 0;

答案 1 :(得分:0)

将Flip函数重写为

private void Flip()
{
   m_FacingRight = !m_FacingRight;
   GetComponent<SpriteRenderer>().flipX = true;
}

此外,如果您正在寻找参考,我在Unity中做了一个平台游戏,其中包括敌人巡逻https://github.com/sean244/Braid-Clone