当前位置:网站首页>【Unity,C#】Character键盘输入转向与旋转
【Unity,C#】Character键盘输入转向与旋转
2022-07-29 10:35:00 【mozhimen】
Character键盘输入转向与旋转
操作键盘反馈Player主角的位移和转向
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lemon : MonoBehaviour
{
public float rotateSpeed = 20f;
//1. 获取玩家输入, 移动角色
//2. 走动的转向问题
//3. 动画问题
private Vector3 _movement;
private float _horizontal;
private float _vertical;
private Rigidbody _rigidbody;
private Animator _animator;
private Quaternion _rotation = Quaternion.identity;//转向初始化为不旋转
void Start()
{
_rigidbody = GetComponent<Rigidbody>();
_animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
_horizontal = Input.GetAxis("Horizontal");
_vertical = Input.GetAxis("Vertical");
}
private void FixedUpdate()
{
refreshMoveMent();
refreshRotation();
refreshAnimation();
}
private void refreshMoveMent()
{
//组装
_movement.Set(_horizontal, 0.0f, _vertical);
_movement.Normalize();
}
private void refreshRotation()
{
Vector3 desiredForward = Vector3.RotateTowards(transform.forward, _movement, rotateSpeed * Time.deltaTime, 0f);
_rotation = Quaternion.LookRotation(desiredForward);
}
private void refreshAnimation()
{
bool isMoveHorizontal = !Mathf.Approximately(_horizontal, 0.0f);
bool isMoveVertical = !Mathf.Approximately(_vertical, 0.0f);
bool isWalking = isMoveHorizontal || isMoveVertical;
_animator.SetBool("IsWalking", isWalking);
}
private void OnAnimatorMove()
{
//动画移动的距离*方向
_rigidbody.MovePosition(_rigidbody.position + _movement * _animator.deltaPosition.magnitude);
_rigidbody.MoveRotation(_rotation);
}
}

边栏推荐
- ADDS:使用 PowerShell 创建 OU 结构
- Using R-Pack premsim to predict microsatellite instability based on gene expression
- Using xgboost with tidymodels
- 跟着田老师学实用英语语法(持续更新)
- 服务器
- Kunlunbase support for MySQL private DML syntax
- [Yugong series] go teaching course 009 in July 2022 - floating point type of data type
- R language brca MRNA data set analysis
- 敏捷开发如何消减协作中的认知偏差?| 敏捷之道
- Kunlunbase instruction manual (III) data import & synchronization
猜你喜欢
![[paper reading] q-bert: Hessian based ultra low precision quantification of Bert](/img/2d/3b9691c16d89dff1a8ac79105172d4.png)
[paper reading] q-bert: Hessian based ultra low precision quantification of Bert

Data visualization design guide (information chart)

How can agile development reduce cognitive bias in collaboration| Agile way

【论文阅读】I-BERT: Integer-only BERT Quantization

SAP Fiori @OData. Analysis of the working principle of publish annotation

基于SSM实现高校后勤报修系统

Two MySQL tables with different codes (utf8, utf8mb4) are joined, resulting in index failure

factoextra:多元统计方法的可视化PCA

Implementation of college logistics repair application system based on SSM

This is the right way for developers to open artifacts
随机推荐
The server
二次握手??三次挥手??
MySQL优化理论学习指南
Docker安装Redis、配置及远程连接
Notes for Resume Writing
Oracle advanced (XIV) explanation of escape characters
Scrape crawler framework
Performance optimization analysis tool | perf
Error: Protobuf syntax version should be first thing in file
[semantic segmentation] 2021-pvt2 cvmj
Kunlunbase support for MySQL private DML syntax
静态资源映射
PAHO cross compilation
软件测试干货
[jetson][reprint]pycharm installed on Jetson
这才是开发者神器正确的打开方式
Using xgboost with tidymodels
If distributed file storage is realized according to integrated Minio
GPO:在 Start/Logon 中使用 PowerShell 脚本
[IVI] 17.1 debugging pit FAQ (compilation)