当前位置:网站首页>【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);
}
}

边栏推荐
- Implementation of college logistics repair application system based on SSM
- 数据可视化设计指南(信息图表篇)
- Evolution of xxl-job architecture for distributed scheduling
- Attachment of text of chenjie Report
- After eating Alibaba's core notes of highly concurrent programming, the backhand rose 5K
- 【论文阅读】Q-BERT: Hessian Based Ultra Low Precision Quantization of BERT
- Summer 2022 software innovation laboratory training JDBC
- VMware: use commands to update or upgrade VMware esxi hosts
- Big cloud service company executives changed: technology gives way to sales
- 会议OA项目(五)---- 会议通知、反馈详情
猜你喜欢

Using Riemann sum to calculate approximate integral in R language

Attachment of text of chenjie Report

站点数据收集-Scrapy使用笔记

R 语言 用黎曼和求近似 积分

Performance optimization analysis tool | perf

Kunlunbase instruction manual (II) best practices for peer-to-peer deployment

Vim到底可以配置得多漂亮?

Efficient 7 habit learning notes
![[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

Hanyuan high tech Gigabit 2-optical 6-conductor rail managed Industrial Ethernet switch supports X-ring redundant ring network one key ring network switch
随机推荐
After eating Alibaba's core notes of highly concurrent programming, the backhand rose 5K
跟着李老师学线代——矩阵(持续更新)
Evolution of xxl-job architecture for distributed scheduling
What are the compensation standards for hospital misdiagnosis? How much can the hospital pay?
Less than 10% of the 3 software test interview questions can be answered correctly! How many do you know?
Ggdag draw DAG and cause and effect diagram
R 语言 用黎曼和求近似 积分
R language uses data set veteran for survival analysis
云服务大厂高管大变阵:技术派让位销售派
HTB-AdmirerToo
基于STM32设计的酒驾报警系统
R 语言 BRCA.mRNA数据集 分析
Achieve the effect of a menu tab
Kunlunbase instruction manual (III) data import & synchronization
架构实战营模块八作业
Why use markdown to write?
This is the right way for developers to open artifacts
Add: create Ou structure using PowerShell
[HFCTF 2021 Final]easyflask
[semantic segmentation] 2021-pvt2 cvmj