当前位置:网站首页>[unity, C #] character keyboard input steering and rotation
[unity, C #] character keyboard input steering and rotation
2022-07-29 10:46:00 【mozhimen】
Character Keyboard input steering and rotation
Operation keyboard feedback Player The displacement and turn of the protagonist
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lemon : MonoBehaviour
{
public float rotateSpeed = 20f;
//1. Get player input , Move character
//2. The steering problem of walking
//3. Animation problems
private Vector3 _movement;
private float _horizontal;
private float _vertical;
private Rigidbody _rigidbody;
private Animator _animator;
private Quaternion _rotation = Quaternion.identity;// Steering is initialized to no rotation
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()
{
// assemble
_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()
{
// The distance the animation moves * Direction
_rigidbody.MovePosition(_rigidbody.position + _movement * _animator.deltaPosition.magnitude);
_rigidbody.MoveRotation(_rotation);
}
}

边栏推荐
- 2022cuda summer training camp Day1 practice
- Follow teacher Li to learn line generation determinant (continuous update)
- How can agile development reduce cognitive bias in collaboration| Agile way
- Less than 10% of the 3 software test interview questions can be answered correctly! How many do you know?
- Atomic operation of day4 practice in 2022cuda summer training camp
- 阿里P8爆出的这份大厂面试指南,看完工资暴涨30k!
- 12代酷睿处理器+2.8K OLED华硕好屏,灵耀14 2022影青釉商务轻薄本
- What are the compensation standards for hospital misdiagnosis? How much can the hospital pay?
- 静态资源映射
- factoextra:多元统计方法的可视化PCA
猜你喜欢

Meeting OA project (V) -- meeting notice and feedback details

Kunlunbase instruction manual (I) quick installation manual

Site data collection -scrapy usage notes

Static resource mapping

mosquitto_ Sub -f parameter use

阿里P8爆出的这份大厂面试指南,看完工资暴涨30k!

静态资源映射

Research on the realization of linear gradient circular progress bar

可线性渐变的环形进度条的实现探究

Kunlunbase instruction manual (III) data import & synchronization
随机推荐
数据可视化设计指南(信息图表篇)
会议OA项目(五)---- 会议通知、反馈详情
Attachment of text of chenjie Report
factoextra:多元统计方法的可视化PCA
一文搞懂什么是二叉树(二叉树的种类、遍历方式、定义)
Big cloud service company executives changed: technology gives way to sales
Kunlun storage vs PostgreSQL OLTP test
2022cuda summer training camp Day6 practice
Less than 10% of the 3 software test interview questions can be answered correctly! How many do you know?
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
Comprehensive and detailed SQL learning guide (MySQL direction)
Introduction to distributed scheduling xxl-job features
使用tidymodels搞定二分类logistic模型
Meeting OA project (V) -- meeting notice and feedback details
[QNX hypervisor 2.2 user manual]7.2.1 hypervisor tracking events
Luogu p4185 [usaco18jan]mootube g problem solution
Conference OA project - my approval
QT工程基本构建
[log frame]
A tour of grp:04 - GRP unary call unary call