当前位置: 首页 > news >正文

Unity 基于Rigidbody2D模块的角色移动

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

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

移动
在这里插入图片描述

角色移动代码如下:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;public class p1_c : MonoBehaviour
{// 获取动画组件private Animator ani;// 获取刚体组件 2Dprivate 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 framevoid 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 -1ani.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);}}
}
http://www.lryc.cn/news/328754.html

相关文章:

  • Stata 15 for Mac:数据统计分析新标杆,让研究更高效!
  • vue配置代理proxy
  • .NET DES加密算法实现
  • 构建操作可靠的数据流系统
  • awesome-cheatsheets:超级速查表 - 编程语言、框架和开发工具的速查表
  • GFW不起作用
  • AndroidStudio出现类似 Could not create task ‘:app:ToolOperatorDemo.main()‘. 错误
  • 一些常见的ClickHouse问题和答案
  • 第九届蓝桥杯大赛个人赛省赛(软件类)真题C 语言 A 组-分数
  • 并发编程——4.线程池
  • 阿里云魔搭发起“ModelScope-Sora开源计划”,将为中国类Sora模型开发提供一站式工具链
  • 大模型与数据分析:探索Text-to-SQL
  • Unity VisionOS开发流程
  • 聊聊k8s服务发现的优缺点
  • Tomcat是如何处理并发请求的?
  • H12-831_561
  • Java23种常见设计模式汇总
  • 神经网络与深度学习(一)
  • 算法学习——LeetCode力扣图论篇2
  • 大数据设计为何要分层,行业常规设计会有几层数据
  • css3之2D转换transform
  • pytest中文使用文档----6临时目录和文件
  • 从0开始搭建基于VUE的前端项目
  • elementUI this.$msgbox msgBox自定义 样式自定义 富文本
  • Lua与Python区别
  • Python学习(二)
  • 管理阿里云服务器ECS -- 网站选型和搭建
  • WPF中继承ItemsControl子类控件数据模板获取选中属性
  • Android卡顿掉帧问题分析之实战篇
  • OpenKylin安装Kafka