Unity-人物移动

2023-11-01

Unity-人物移动

人物模型

参考以下视频:

如何在Unity中导入pmx格式的MMD模型_哔哩哔哩_bilibili

用的是原神模型,这里要注意导入后把人物模型的Rig换为Humanoid

人物动作

使用的Unity-Chan! Model的动作,在asset store找到这个资源添加并导入就行。

设置动画

使用blend-tree混合人物移动的动画

image-20220917192717665

image-20220917192732181

代码脚本编写

输入系统

  • 安装插件

image-20220917192751129

  • 为Input Actions创建按键映射

在这之前要创建Input Actions,并 勾选生成C#类

image-20220917192813882

  • 脚本控制输入
using System;
using System.Collections;
using System.Collections.Generic;
using System.Input;
using UnityEngine;

namespace Player.Common
{
    /**
     * 输入系统
     */
     public class InputSystem : MonoBehaviour
    {
        [Header("移动")]
        [HideInInspector]public float horizontal;
        [HideInInspector]public float vertical;
        [HideInInspector]public float moveAmount;
        [HideInInspector]public bool isRun;

        // private float mouseX;
        // private float mouseY;


        private InputControls inputControls;

        private Vector2 movementInput;
        [HideInInspector]
        public bool jumpInput;
        private Vector2 cameraInput;
        public void OnEnable()
        {
            if (inputControls == null)
            {
                inputControls = new InputControls();

                inputControls.Player.Move.performed +=
                    inputControls => movementInput = inputControls.ReadValue<Vector2>();
                inputControls.Player.Jump.performed +=
                    inputControls => jumpInput = inputControls.ReadValueAsButton();
                inputControls.Player.Run.performed +=
                    inputControls => isRun = inputControls.ReadValueAsButton();

                // inputControls.PlayerMovment.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
            }

            inputControls.Enable();
        }

        public void OnDisable()
        {
            inputControls.Disable();
        }

        public void UpdateMoveInput()
        {
            horizontal = movementInput.x;
            vertical = movementInput.y;
            moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));

            // mouseX = cameraInput.x;
            // mouseY = cameraInput.y;
        }


    }

}

动画脚本


using UnityEngine;

namespace Player.Animation
{
    public class AnimationHandler : MonoBehaviour
    {
        [HideInInspector]
        public Animator anim;

        [HideInInspector]public bool IsRun;
        [HideInInspector]public bool IsWalk;

        private int vertical;
        private int horizontal;

        public bool canRotate = true;

        public void Initialize()
        {
            anim = GetComponent<Animator>();
            vertical = Animator.StringToHash("Vertical");
            horizontal = Animator.StringToHash("Horizontal");
        }

        public void UpdateAnimatorValues(bool isWalk,bool isRun,float verticalMovement, float horizontalMovement)
        {

            anim.SetBool("IsRun",isRun);
            anim.SetBool("IsWalk",isWalk);
            // if (isRun)
            // {
                verticalMovement = Mathf.Abs(verticalMovement);
                horizontalMovement = Mathf.Abs(horizontalMovement);
                anim.SetFloat(vertical,verticalMovement,0.1f,Time.deltaTime);
                anim.SetFloat(horizontal,horizontalMovement,0.1f,Time.deltaTime);
            // }

        }

        public void CanRotate()
        {
            canRotate = true;
        }

        public void StopRotation()
        {
            canRotate = false;
        }

    }

}

人物引擎


using Player.Animation;
using UnityEngine;

namespace Player.Common
{
    public class PlayerMotor : MonoBehaviour
    {
        [Header("基本属性")]
        private InputSystem inputSystem;
        private Rigidbody rigidbody;
        [HideInInspector]
        public Transform playerTransForm;
        [HideInInspector]
        public AnimationHandler animationHandler;

        [Header("移动参数")]
        public float WalkSpeed = 5f;
        public float RunSpeed = 12f;
        private Vector3 moveDirection;
        public float RotateSpeed = 5f;

        void Start()
        {
            inputSystem = GetComponent<InputSystem>();
            rigidbody = GetComponent<Rigidbody>();
            playerTransForm = GetComponent<Transform>();
            animationHandler = GetComponentInChildren<AnimationHandler>();
        }

        void Update()
        {
            PlayerMove();
            PlayerRotate(moveDirection);

        }

        private void PlayerMove()
        {
            inputSystem.UpdateMoveInput();
            moveDirection = new Vector3(inputSystem.horizontal,0,inputSystem.vertical);
            moveDirection *= WalkSpeed;
            rigidbody.velocity = moveDirection;


            animationHandler.Initialize();
            bool  isWalk = (Mathf.Abs(inputSystem.horizontal) > 0 || Mathf.Abs(inputSystem.vertical) > 0) ? true : false;

            animationHandler.UpdateAnimatorValues(isWalk,inputSystem.isRun,inputSystem.vertical,inputSystem.horizontal);
        }

        private void PlayerRotate(Vector3 target)
        {
            if (Vector3.zero.Equals(target)) return;

            Quaternion tr = Quaternion.LookRotation(target);
            Quaternion targetRotation = Quaternion.Slerp(playerTransForm.rotation,tr,RotateSpeed*Time.deltaTime);
            playerTransForm.rotation = targetRotation;

        }
    }
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Unity-人物移动 的相关文章

随机推荐

  • 数据解析神器 parsel库

    parsel库的基本使用 parsel是一个python的第三方库 相当于css选择器 xpath re parsel由scrapy团队开发 是将scrapy中的parsel独立抽取出来的 可以轻松解析html xml内容 获取需要的数据
  • 寒假作业【主席树】

    题目链接 P2717 寒假作业 题目要求的是平均值不小于K的 那么可以将问题变成 对所有的都减去K 然后求 权值和大于等于0 的子串的个数有多少个 于是 我们可以求 以每个点作为子串结尾的点时候的可能的子串的数量 这里就可以用前缀和来维护了
  • muduo net库学习笔记2——muduo网络库相关类图的关系、EventLoop、Channel、 Poller

    EventLoop的简化封装 有在上篇文章中出现过但是连贯一下就还是搬过来 看完类图关系就可以分析完善的代码了 可以直接跳转到正文类图关系 h文件 namespace muduo namespace net Reactor at most
  • 【算法】蛇形填数

    题目描述如下 思路 输入n 构建一个n n的矩阵 初始化所有值为为0 加头文件 include
  • xp系统显示无打印机服务器,XP共享打印机时提示“工作站服务没有启动”的原因和解决方案...

    很多WindowsXP系统用户在日常办公时 经常会碰到需要共享打印机的情况 不过 xp系统共享打印机时偶尔也会提示 工作站服务没有启动 这是怎么回事呢 下面 小编就给大家介绍XP共享打印机时提示 工作站服务没有启动 的原因和解决方案 原因分
  • 初学Java该学哪些知识?这6大知识必学

    目前 Java是开发人员的热宠 很多论坛都有不少热爱Java的开发人员 也有不少想成为Java程序员 但苦于不知道该如何学习Java 也不清楚该学些什么知识才能成为一个Java程序员 小千在这里抛砖引玉 和大家讨论初学Java应该掌握的知识
  • gitee配置ssh后仍需要密码

    gitee创建仓库后默认提供的是https链接需要修改为ssh才能免密登录 1 查看远程仓库链接 git remote v 删除远程仓库 git remote rm origin 重新添加远程仓库 ssh地址 git remote add
  • 关于hive中从hdfs上load数据到表中而HDFS上的数据却消失的若干问题

    原链接 https blog csdn net shuaikang666 article details 80357075 今天偶然间发现hive中一个我之前没有注意到的一个小细节 我怀疑你们之前也可能没有注意到 那就是当我们试图从HDFS
  • Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

    catalog 1 How to Add New Functions to MySQL 2 Features of the User Defined Function Interface 3 User Defined Function 4
  • postgres数据库相关使用说明

    默认的数据库和用户名是postgres 登录 psql U postgres d postgres ctrl c q 退出数据库交互模式 创建新用户 gwp createuser U postgres P d gwp 输入密码 mxq123
  • 路由器和交换机工作原理

    路由器工作原理 路由器 三层设备 同时基于二层设备工作 当数据包进到路由器时 首先查看的是二层报头 查看的是目标MAC 目标MAC分为三种 广播 组播 单播 广播地址 解封装到三层报头 组播地址 每一个组播地址均存在自己的MAC地址 基于目
  • 华为OD题目: 任务总执行时长

    package com darling boot order od od10 import com sun org apache bcel internal generic IF ACMPEQ import java util 任务总执行时
  • 几种I/O编程实践

    1 传统的BIO编程 网络编程的基本模型是Client Server模型 也就是两个进程间相互通信 其中 服务端提供位置信息 绑定的IP地址和监听端口 客户端提供连接操作向服务端监听的地址发起连接请求 通过三次握手建立连接 如果连接建立成功
  • Burpsuite在Firefox中无法抓取DVWA本地数据包解决方案+导入证书

    前言 这几天重装了系统 软件也大部分重新安装 在使用bp时 遇到了不能抓取dvwa数据包的情况 解决方案 猜想 可能是浏览器自动将127 0 0 1与localhost默认选择不使用代理服务 无法修改 反正我没找到 方案 将url栏中的12
  • java计算下一个整5分钟时间点

    需求 需要获取当前时间的下一个整点时间 如13 23 获取的下一个时间为 13 25 代码 获取下一个分钟值以0或者5结尾的时间点 单位 毫秒 return public static long getNextMillisEndWithMi
  • 机器数——源码、反码、补码

    机器数 源码 反码 补码 基本定义 1 机器数是将符号 数字化 的数 是数字在计算机中的二进制表示形式 表示一个机器数 应该考虑以下三个因素 1 机器数的范围 2 机器数的符号 3 机器数中小数点的位置 我们这里只讨论二进制整数在计算机中的
  • 【Java筑基】IO流基础之常见工具流和进程通信

    前 言 作者简介 半旧518 长跑型选手 立志坚持写10年博客 专注于java后端 专栏简介 深入 全面 系统的介绍java的基础知识 文章简介 本文将深入全面介绍IO流知识 建议收藏备用 创作不易 敬请三连哦 大厂真题 大厂面试真题大全
  • Python3 入门及基础语法

    文章目录 解释型语言 解释型语言优缺点 和编译性语言的区别 Python 简介 优点 缺点 和其他语言区别 Python 入门 Python 解释器安装 Python 继承开发环境安装 第一个 Python 程序 Python 基础 注释
  • MySql的时区(serverTimezone)引发的血案

    前言 mysql8 x的jdbc升级了 增加了时区 serverTimezone 属性 并且不允许为空 血案现场 配置jdbc的URL jdbc mysql IP PORT DB characterEncoding utf8 useSSL
  • Unity-人物移动

    Unity 人物移动 人物模型 参考以下视频 如何在Unity中导入pmx格式的MMD模型 哔哩哔哩 bilibili 用的是原神模型 这里要注意导入后把人物模型的Rig换为Humanoid 人物动作 使用的Unity Chan Model