如何解决如何将Lerp从未初始化的值转换为初始化的值?
当inputDirection
尚未初始化(仅定义)时,如何将targetInput
转换为inputDirection
感到困惑。这段代码来自一个教程,我知道它可以工作,但是我不知道为什么。我的猜测是inputDirection
是Vector3
的一种特殊类型,它是对用户输入的引用,或者Vector3
的默认值为(0,0,0),但是我的研究是到目前为止还没有成功。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
private PlayerInputActions inputActions;
private Vector2 movementInput;
[SerializeField]
private float moveSpeed = 10f;
private Vector3 inputDirection;
private Vector3 moveVector;
private Quaternion currentRotation;
void Awake()
{
inputActions = new PlayerInputActions();
inputActions.Player.Movement.performed += context => movementInput = context.ReadValue<Vector2>();
//Reads a Vector2 value from the action callback,stores it in the movementInput variable,and adds movementInput to the performed action event of the player
}
void FixedUpdate()
{
float h = movementInput.x;
float v = movementInput.y;
//y value in the middle
//0 since no jump
Vector3 targetInput = new Vector3(h,v);
inputDirection = Vector3.Lerp(inputDirection,targetInput,Time.deltaTime * 10f);
}
}
解决方法
Vector3
是struct
(不是class
),这意味着其中的所有字段均使用其默认值初始化。这就是为什么如果您自己不使用其他值初始化的原因,则为(0,0,0)(因为默认值float
是x
,y
的类型)而z
为0)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。