Unity2Dでアクションゲームを作る(願望)その4

初めに

はい、こんにちは。黒夢です。今日は少し出かけていたので、そこまで進まない気がします。あぁ…進捗が…。
それはともかく、今日も今日とてやっていきましょう!今日やることは…そう、他のアニメーションへの遷移ですね。

落下・走るアニメーションの追加

まず、今のコードの確認です。

    private Animator anim = null;
    public Vector2 Speed;
    public Vector2 Acceleration;
    public float Gravity;
    public player_ground_contact GroundContact;

    void Start()
    {
        anim = GetComponent<Animator>();
        Gravity = -0.3f;
        Speed = new Vector2(0, 0);
        Acceleration = new Vector2(0, Gravity);
    }

    void Update()
    {
    }

    void FixedUpdate()
    {
        float horizontalKey = Input.GetAxis("Horizontal");
        Rigidbody2D PComponent = GetComponent<Rigidbody2D>();

        if (horizontalKey > 0)
        {
            if (!anim.GetBool("RorL"))
                anim.SetBool("RorL", true);
            anim.SetInteger("Action", 1);
            Acceleration.x = 0.2f;
        }
        else if (horizontalKey < 0)
        {
            if (anim.GetBool("RorL"))
                anim.SetBool("RorL", false);
            anim.SetInteger("Action", 1);
            Acceleration.x = -0.2f;
        }
        else
        {
            anim.SetInteger("Action", 0);
            Speed.x = 0;
            Acceleration.x = 0;
        }

        if (GroundContact.IsField())
        {
            Speed.y = 0;
            Acceleration.y = 0;
        }
        else
            Acceleration.y = Gravity;

        Speed += Acceleration;
        PComponent.velocity = Speed;
    }

こんな感じですね。それでは、書いていきましょう。

public class player_script : MonoBehaviour
{
    private Animator anim = null;
    public Vector2 Speed;
    public Vector2 Acceleration;
    public float Gravity;
    public player_ground_contact GroundContact;

    void Start()
    {
        anim = GetComponent<Animator>();
        Gravity = -0.3f;
        Speed = new Vector2(0, 0);
        Acceleration = new Vector2(0, Gravity);
    }

    void Update()
    {
    }

    void FixedUpdate()
    {
        float horizontalKey = Input.GetAxis("Horizontal");
        Rigidbody2D PComponent = GetComponent<Rigidbody2D>();

        if (horizontalKey > 0)
        {
            anim.SetBool("RorL", true);
            Acceleration.x = 0.2f;
        }
        else if (horizontalKey < 0)
        {
            anim.SetBool("RorL", false);
            Acceleration.x = -0.2f;
        }
        else
        {
            Speed.x = 0;
            Acceleration.x = 0;
        }

        if (GroundContact.IsField())
        {
            Speed.y = 0;
            Acceleration.y = 0;
        }
        else
            Acceleration.y = Gravity;

        if(Speed.x > 0)
            anim.SetBool("RorL", true);
        else if(Speed.x < 0)
            anim.SetBool("RorL", false);

        if (GroundContact.IsField() == false)
            anim.SetInteger("Action", 3);
        else if (Mathf.Abs(Speed.x) > 5)
        {
            anim.SetInteger("Action", 1);
            anim.SetBool("Run", true);
        }
        else if (Mathf.Abs(Speed.x) > 0)
        {
            anim.SetInteger("Action", 1);
            anim.SetBool("Run", false);
        }
        else
        {
            anim.SetInteger("Action", 0);
            anim.SetBool("Run", false);
        }


        Speed += Acceleration;
        PComponent.velocity = Speed;
    }
}
書けました。が、少し動きがおかしい…?<br>
続きはまた明日にします。それでは、良い黒の夢が見れますように…。<br>