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);
        }
  
    }
}

相关推荐

  1. Unity2D_角色移动&跳跃

    2024-03-31 06:00:04       35 阅读
  2. Unity3D 基于AStar地图摇杆控制角色详解

    2024-03-31 06:00:04       20 阅读
  3. Unity相机跟随角色移动

    2024-03-31 06:00:04       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-31 06:00:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-31 06:00:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-31 06:00:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-31 06:00:04       20 阅读

热门阅读

  1. 专升本-机器人流程化自动化(RPA)

    2024-03-31 06:00:04       20 阅读
  2. Kafka开机自启脚本

    2024-03-31 06:00:04       19 阅读
  3. vector和array区别

    2024-03-31 06:00:04       20 阅读
  4. IPv6 Scapy Samples

    2024-03-31 06:00:04       15 阅读
  5. 2024-03-30 问AI: 介绍一下深度学习里面的 DCNN模型

    2024-03-31 06:00:04       18 阅读
  6. WebStorm 与 VSCode 对比分析

    2024-03-31 06:00:04       17 阅读
  7. 【微服务篇】深入理解RPC(远程调用)原理

    2024-03-31 06:00:04       17 阅读