Unity 基于Rigidbody2D模块的角色移动

制作好站立和移动的动画后

控制器设计
在这里插入图片描述
站立
在这里插入图片描述

移动
在这里插入图片描述

角色移动代码如下:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class p1_c : MonoBehaviour
{


    // 获取动画组件
    private Animator ani;
    // 获取刚体组件 2D
    private Rigidbody2D rBody;

    // 移动方向
    Vector2 dir = new Vector2(0, 0);
    // 站立方向
    Vector2 h_v = new Vector2(0, 0);

    // 10 右  -10 左 
    // 01 上  0-1 下 

    void Start()
    {
        

        // 加载两个组件
        ani = GetComponent<Animator>();
        rBody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        //float h = Input.GetAxis("Horizontal");
        //float v = Input.GetAxis("Vertical");

        // 10 右  -10 左 
        // 01 上  0-1 下 

        // 左
        if (Input.GetKeyDown(KeyCode.A) || (Input.GetKey(KeyCode.A)))
        {
            dir = dir + new Vector2(-1, 0);
            h_v = new Vector2(-1, 0);
        }

        // 右
        if (Input.GetKeyDown(KeyCode.D) || (Input.GetKey(KeyCode.D)))
        {
            dir = dir + new Vector2(1, 0);
            h_v =  new Vector2(1, 0);
        }

        // 上
        if (Input.GetKeyDown(KeyCode.W) || (Input.GetKey(KeyCode.W)))
        {
            dir = dir + new Vector2(0, 1);
            h_v = new Vector2(0, 1);
        }
        // 下
        if (Input.GetKeyDown(KeyCode.S) || (Input.GetKey(KeyCode.S)))
        {
            dir = dir + new Vector2(0, -1);
            h_v =  new Vector2(0, -1);
        }



        if ((dir[0] != 0) || (dir[1] != 0))
        {
            if (dir[0] > 1)
            {
                dir[0] = 1;
            }
            if (dir[0] < -1)
            {
                dir[0] = -1;
            }

            if (dir[1] > 1)
            {
                dir[1] = 1;
            }
            if (dir[1] < -1)
            {
                dir[1] = -1;
            }
        }

        
        if ((h_v[0] != 0) || (h_v[1] != 0))
        {
            if (h_v[0] > 1)
            {
                h_v[0] = 1;
            }
            if (h_v[0] < -1)
            {
                h_v[0] = -1;
            }

            if (h_v[1] > 1)
            {
                h_v[1] = 1;
            }
            if (h_v[1] < -1)
            {
                h_v[1] = -1;
            }
        }
        
        // 右 10 左 -1 0
        // 上 11 下 0 -1
        ani.SetFloat("H", h_v[0]);
        ani.SetFloat("V", h_v[1]);
        // 刚体 方向速度
        rBody.velocity = dir * 1f;
        // 获取
        ani.SetFloat("速度", dir.magnitude);


        // 松开
        if (Input.GetKeyUp(KeyCode.A) ||
            Input.GetKeyUp(KeyCode.W) ||
            Input.GetKeyUp(KeyCode.S) ||
            Input.GetKeyUp(KeyCode.D))
        {
            //Debug.Log("松开AWSD键");
            dir = new Vector2(0, 0);
        }
  
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

默执_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值