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

边栏推荐
- Research on the realization of linear gradient circular progress bar
- 98. (cesium chapter) cesium point heat
- Big cloud service company executives changed: technology gives way to sales
- HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
- Two MySQL tables with different codes (utf8, utf8mb4) are joined, resulting in index failure
- Error: Protobuf syntax version should be first thing in file
- Kunlunbase instruction manual (IV) real time synchronization of data from Oracle to kunlunbase
- Scrape crawler framework
- Second handshake?? Three waves??
- R language uses data set veteran for survival analysis
猜你喜欢

Easy to understand and explain the gradient descent method!

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

Comprehensive and detailed SQL learning guide (MySQL direction)

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

If distributed file storage is realized according to integrated Minio
![[dark horse morning post] Youxian responded to the dissolution every day, and many places have been unable to place orders; Li Bin said that Wei Lai will produce a mobile phone every year; Li Ka Shing](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] Youxian responded to the dissolution every day, and many places have been unable to place orders; Li Bin said that Wei Lai will produce a mobile phone every year; Li Ka Shing

Learning R language these ebooks are enough!

Understanding of Arduino circuit

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

这才是开发者神器正确的打开方式
随机推荐
一文搞懂什么是二叉树(二叉树的种类、遍历方式、定义)
Conference OA project - my approval
ggdag 绘制DAG和因果图
VMWare:使用命令更新或升级 VMWare ESXi 主机
通过tidymodels使用XGBOOST
若依集成minio实现分布式文件存储
DW: optimize the training process of target detection and more comprehensive calculation of positive and negative weights | CVPR 2022
这才是开发者神器正确的打开方式
Using Riemann sum to calculate approximate integral in R language
Add: create Ou structure using PowerShell
Roots of equations in R language dichotomy and Newton iteration
How can agile development reduce cognitive bias in collaboration| Agile way
Ggdag draw DAG and cause and effect diagram
factoextra:多元统计方法的可视化PCA
Create PHP message board system with kubernetes
Luogu p1816 loyalty solution
R language Monte Carlo method and average method are used to calculate the definite integral. Considering the random point casting method, the confidence is 0.05, and the requirement is ϵ= 0.01, numbe
Kunlunbase instruction manual (III) data import & synchronization
Research on the realization of linear gradient circular progress bar
Consumer electronics, frozen to death in summer