転職を繰り返したサラリーマンの多趣味ブログ

30才未経験でSEに転職した人の多趣味ブログ

【Unity】敵の移動例

public LayerMask blockLayer;

private Rigidbody2D rbody;

private float moveSpeed = 1;

public enum MOVE_DIR
{
    LEFT,
    RIGHT,
};

private MOVE_DIR moveDirection = MOVE_DIR.LEFT;

private void FixedUpdate()
    {
        bool isBlock;

        switch(moveDirection)
        {
            case MOVE_DIR.LEFT:
                rbody.velocity = new Vector2(moveSpeed * -1, rbody.velocity.y);
                transform.localScale = new Vector2(1, 1);

                isBlock = Physics2D.Linecast(
                    new Vector2(transform.position.x, transform.position.y + 0.5f),
                    new Vector2(transform.position.x - 0.3f, transform.position.y + 0.5f),
                    blockLayer);

                if(isBlock)
                {
                    moveDirection = MOVE_DIR.RIGHT;
                }

                break;
            case MOVE_DIR.RIGHT:
                rbody.velocity = new Vector2(moveSpeed, rbody.velocity.y);
                transform.localScale = new Vector2(-1, 1);

                isBlock = Physics2D.Linecast(
                    new Vector2(transform.position.x, transform.position.y + 0.5f),
                    new Vector2(transform.position.x + 0.3f, transform.position.y + 0.5f),
                    blockLayer);

                if (isBlock)
                {
                    moveDirection = MOVE_DIR.LEFT;
                }
                break;
        }
    }