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

边栏推荐
- [QNX hypervisor 2.2 user manual]7.2.1 hypervisor tracking events
- 【Unity,C#】Character键盘输入转向与旋转
- [reading notes] the way of enterprise IT architecture transformation Alibaba's China Taiwan strategic thinking and Architecture Practice
- HTB-AdmirerToo
- Factoextra: visualization of multivariate statistics
- Structure the eighth operation of the combat battalion module
- 重磅 | 2022 开放原子全球开源峰会在北京开幕
- 会议OA项目----我的审批
- Atomic operation of day4 practice in 2022cuda summer training camp
- Is there any charge for PDF processing? impossible
猜你喜欢

美团、饿了么被杭州市监约谈要求落实食品安全管理责任 严禁恶意竞争

ECCV 2022 | CMU proposes to recurse on the visual transformer without adding parameters, and the amount of calculation is still small

12th generation core processor +2.8k OLED ASUS good screen, lingyao 142022 shadow cyan glaze business thin book

DW: optimize the training process of target detection and more comprehensive calculation of positive and negative weights | CVPR 2022

Consumer electronics, frozen to death in summer

浅谈安科瑞灭弧式智慧用电在养老机构的应用

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

remap_ Use of table in impdp

主子仓库都修改,如何进行同步?

Tell you from my accident: Mastering asynchrony is key
随机推荐
周鸿祎:360是世界上最大的安全大数据公司
paho交叉编译
皕杰报表之文本附件属件
Software testing dry goods
Kunlunbase instruction manual (II) best practices for peer-to-peer deployment
Factoextra: visualization of multivariate statistics
浅谈安科瑞灭弧式智慧用电在养老机构的应用
Using R-Pack premsim to predict microsatellite instability based on gene expression
How to synchronize when the primary and sub warehouses are modified?
Alibaba P8 broke out this interview guide for big factories. After reading it, the salary soared by 30K!
A tour of grp:04 - GRP unary call unary call
Explore SQL Server metadata (I)
What is the difference between a global index and a local index?
QT基本工程的解析
Error: Protobuf syntax version should be first thing in file
HTB-AdmirerToo
一文搞懂什么是二叉树(二叉树的种类、遍历方式、定义)
8.穿插-从架构设计到实践理解ThreadPoolExecutor线程池
Scrape crawler framework
Introduction to distributed scheduling xxl-job features