Unity:当我释放动作输入时,玩家行为怪异
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity:当我释放动作输入时,玩家行为怪异相关的知识,希望对你有一定的参考价值。
大家好!我正在尝试让我的玩家面对他走来的方向,但是奇怪的事情开始发生了。
我试图让我的玩家面对他行走的方向,但随后奇怪的事情开始发生。每当我现在松开输入键,玩家就会慢慢被吸到他的本地Z轴上。有时候他站着的时候会这样,有时候他翻转的时候也会这样。
任何帮助都将是非常感激的!
先谢谢你
我会把我的脚本提供给你。
public class JackMovement3D : MonoBehaviour {
public float speed = 6.0F;
public float VerticalSpeed = 10f;
public float HorizontalSpeed = 60f;
public float rotationSpeed = 5f;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private Animator animator;
private void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded)
{
float horizontal = Input.GetAxis("Horizontal") * HorizontalSpeed;
float vertical = Input.GetAxis("Vertical") * VerticalSpeed;
//Feed moveDirection with input.
moveDirection = new Vector3(horizontal, 0, vertical);
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
animator.SetFloat("Blend", controller.velocity.magnitude);
//Look at walking direction
Quaternion newRotation = Quaternion.LookRotation(moveDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, rotationSpeed);
// CharacterController.Move to move the player in target direction
controller.Move(moveDirection * Time.deltaTime);
}
private void LateUpdate()
{
transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, transform.localEulerAngles.z);
}
}
答案
不知道这是否是你所描述的问题,但可能与以下问题有关。
Quaternion.LookRotation(moveDirection);
因为如果没有输入,则 moveDirection
是一个 Vector3(0,0,0)
和 Quaternion.LookRotation
如果向前或向上的幅度为零,则返回身份。
所以你可能可以通过添加一个检查来避免将旋转重置为idendity,如
// if movement is 0 do nothing else
if(Mathf.Approximately(moveDirection.sqrMagnitude, 0)) return;
这应该是在添加重力之前。
其实我也不太确定,但我猜测重力应该是在加完
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.isGrounded)
{
float horizontal = Input.GetAxis("Horizontal") * HorizontalSpeed;
float vertical = Input.GetAxis("Vertical") * VerticalSpeed;
//Feed moveDirection with input.
moveDirection = new Vector3(horizontal, 0, vertical);
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
}
animator.SetFloat("Blend", controller.velocity.magnitude);
// if movement is 0 do nothing else
if(Mathf.Approximately(moveDirection.sqrMagnitude, 0))
{
return;
}
moveDirection.y -= gravity * Time.deltaTime;
//Look at walking direction
var newRotation = Quaternion.LookRotation(moveDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, rotationSpeed);
// CharacterController.Move to move the player in target direction
controller.Move(moveDirection * Time.deltaTime);
}
以上是关于Unity:当我释放动作输入时,玩家行为怪异的主要内容,如果未能解决你的问题,请参考以下文章
释放 LongPressGesture SwiftUI 时触发动作