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

边栏推荐
- What are the compensation standards for hospital misdiagnosis? How much can the hospital pay?
- R language brca MRNA data set analysis
- Conference OA project - my approval
- R 语言 Monte Carlo方法 和平均值法 计算定积分, 考虑随机投点法,计算在置信度0.05, 要求为ϵ=0.01 , 所需要的试验次数
- The server
- Follow teacher Li to learn line generation determinant (continuous update)
- Static resource mapping
- Evolution of xxl-job architecture for distributed scheduling
- Kunlunbase instruction manual (I) quick installation manual
- How to realize the function of adding watermark
猜你喜欢

Kunlunbase instruction manual (IV) real time synchronization of data from Oracle to kunlunbase

Site data collection -scrapy usage notes

转转push的演化之路

NUMA architecture CPU API change summary
![[HFCTF 2021 Final]easyflask](/img/58/8113cafae8aeafcb1c9ad09eefd30f.jpg)
[HFCTF 2021 Final]easyflask

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

Second handshake?? Three waves??

How to synchronize when the primary and sub warehouses are modified?

Conference OA project - my approval

DW: optimize the training process of target detection and more comprehensive calculation of positive and negative weights | CVPR 2022
随机推荐
Data visualization design guide (information chart)
ADB shell WM command and usage:
R 语言 BRCA.mRNA数据集 分析
R 语言 Monte Carlo方法 和平均值法 计算定积分, 考虑随机投点法,计算在置信度0.05, 要求为ϵ=0.01 , 所需要的试验次数
基于STM32设计的酒驾报警系统
[semantic segmentation] 2021-pvt iccv
[log frame]
使用R包PreMSIm根据基因表达量来预测微卫星不稳定
Software testing dry goods
StarRocks 技术内幕:实时更新与极速查询如何兼得
Vim到底可以配置得多漂亮?
Using xgboost with tidymodels
R language uses data set veteran for survival analysis
2022cuda summer training camp Day1 practice
Factoextra: visual PCA of multivariate statistical methods
Leetcode binary tree series -- 144. Preorder traversal of binary trees
会议OA项目(五)---- 会议通知、反馈详情
Svn revision keyword
WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
Error: Protobuf syntax version should be first thing in file