Walk.cs 1.44 KB
Newer Older
Jordan B Mckenney's avatar
Jordan B Mckenney committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walk : AbstractBehavior {
    public int speed;
    private float airSpeed;
    public float airAcceleration;

    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {
        Buttons left = inputButtons[0];
        Buttons right = inputButtons[1];

        int rightInt = 0;
        int leftInt = 0;

        if (inputState.GetButtonValue(right))
            rightInt = 1;
        else
            rightInt = 0;

        if (inputState.GetButtonValue(left))
            leftInt = -1;
        else
            leftInt = 0;

        if (collisionState.onGround)
        {
            body.velocity = new Vector2(speed * (rightInt + leftInt), body.velocity.y);
        } else
        {
            airSpeed = body.velocity.x;
            if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left))
            {
                airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
            }
            else
            {
                if (airSpeed != 0) {
                    airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
                    if (Mathf.Abs(airSpeed) < airAcceleration)
                        airSpeed = 0;
                }
            }
                body.velocity = new Vector2(airSpeed, body.velocity.y);
        }
    }
}