【Unity】控制人物左右移动和跑步的实现

通过判断shift按下和左右键按下进行移动角色,设置跑步和走路速度。

//字面意思的状态
enum playerGroundStateEnum
{
    stand = 0,
    walk = 1,
    run = 2
}

public class PlayerController : MonoBehaviour
{
    public float playerSpeed = 5f;
    //走路移动速度
    public float walkSpeed = 5f;
    //跑步移动速度
    public float runningSpeed = 8f;
    private bool _isFacingRight=true;
    private bool isFacingRight
    {
        get
        {
            return _isFacingRight;
        }
        set
        {
        	//如果当前方向与新赋值的方向不一致则转向
            if (_isFacingRight != value)
            {
                transform.localScale *= new Vector2(-1, 1);
            }
            _isFacingRight = value;
        }
    }
    private Rigidbody2D body;
    private Vector2 inputControll;
    private bool isWalking;
    private bool isShitfDonw;
    public int playerGroundState
    {
        get
        {
            return anim.GetInteger(AnimationString.GroundState);
        }
        set
        {
            anim.SetInteger(AnimationString.GroundState, value);
        }
    }

    Animator anim;
    // Start is called before the first frame update
    void Start()
    {

    }
    private void Awake()
    {
        body = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {

    }
    private void FixedUpdate()
    {
        body.velocity = new Vector2(inputControll.x * playerSpeed, body.velocity.y);

        if (isWalking)
        {
            if (isShitfDonw)
            {
                setPlayerRuning();
            }
            else
            {
                setPlayerWalk();
            }
        }
        else
        {
            setPlayerStand();
        }
    }

    private void setPlayerWalk()
    {
        playerSpeed = walkSpeed;
        playerGroundState = (int)playerGroundStateEnum.walk;
    }

    private void setPlayerStand()
    {
        playerSpeed = walkSpeed;
        playerGroundState = (int)playerGroundStateEnum.stand;
    }

    private void setPlayerRuning()
    {
        playerSpeed = runningSpeed;
        playerGroundState = (int)playerGroundStateEnum.run;
    }
	//给角色绑定的移动事件
    public void onMove(InputAction.CallbackContext context)
    {
        inputControll = context.ReadValue<Vector2>();
        isWalking = inputControll != Vector2.zero;
        if (inputControll.x > 0 && !isFacingRight)
        {
            isFacingRight = true;
        }
        else if (inputControll.x < 0 && isFacingRight)
        {
            isFacingRight = false;
        }
    }
	//给角色绑定的左Left事件
    public void onRun(InputAction.CallbackContext context)
    {
        if (context.started)
        {
            isShitfDonw = true;
        }
        else if (context.canceled)
        {
            isShitfDonw = false;
        }
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值