Ejemplo n.º 1
0
    public void Move()
    {
        Vector2 playerPosition = GetPlayerPosition();

        // move towards player
        // flip sprite to the right (original orientation)
        // if player X is greater than enemy X position
        if (transform.position.x < playerPosition.x - 0.5f)
        {
            transform.rotation = Quaternion.Euler(0, 0, 0);
            spriteAnimator.AnimateRun();

            rigidbody.velocity = new Vector2(movementSpeed, rigidbody.velocity.y);
        }
        else if (playerPosition.x + 0.5f < transform.position.x)
        {
            transform.rotation = Quaternion.Euler(0, 180, 0);
            spriteAnimator.AnimateRun();

            rigidbody.velocity = new Vector2(-movementSpeed, rigidbody.velocity.y);
        }
        else
        {
            spriteAnimator.AnimateIdle();
            rigidbody.velocity = new Vector2(0, 0);
        }
    }
Ejemplo n.º 2
0
    private void Move()
    {
        if (Input.GetKey(KeyCode.A))
        {
            transform.rotation = Quaternion.Euler(0, 180, 0);

            if (IsGrounded())
            {
                m_SpriteAnimator.AnimateRun();
                m_Rigidbody2d.velocity = new Vector2(-m_MovementSpeed, m_Rigidbody2d.velocity.y);
            }
            else
            {
                m_Rigidbody2d.velocity += new Vector2(-m_MovementSpeed * m_MidAirSpeed * Time.deltaTime, 0);
                m_Rigidbody2d.velocity  = new Vector2(Mathf.Clamp(m_Rigidbody2d.velocity.x, -m_MovementSpeed, m_MovementSpeed), m_Rigidbody2d.velocity.y);
            }
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.rotation = Quaternion.Euler(0, 0, 0);

            if (IsGrounded())
            {
                m_SpriteAnimator.AnimateRun();
                m_Rigidbody2d.velocity = new Vector2(m_MovementSpeed, m_Rigidbody2d.velocity.y);
            }
            else
            {
                m_Rigidbody2d.velocity += new Vector2(m_MovementSpeed * m_MidAirSpeed * Time.deltaTime, 0);
                m_Rigidbody2d.velocity  = new Vector2(Mathf.Clamp(m_Rigidbody2d.velocity.x, -m_MovementSpeed, m_MovementSpeed), m_Rigidbody2d.velocity.y);
            }
        }
        else
        {
            if (IsGrounded())
            {
                m_SpriteAnimator.AnimateIdle();
                m_Rigidbody2d.velocity = new Vector2(0, m_Rigidbody2d.velocity.y);
            }
        }
    }