如何将Lerp从未初始化的值转换为初始化的值?

问题描述

inputDirection尚未初始化(仅定义)时,如何将targetInput转换为inputDirection感到困惑。这段代码来自一个教程,我知道它可以工作,但是我不知道为什么。我的猜测是inputDirectionVector3的一种特殊类型,它是对用户输入的引用,或者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);
  }
 }

解决方法

Vector3struct(不是class),这意味着其中的所有字段均使用其默认值初始化。这就是为什么如果您自己不使用其他值初始化的原因,则为(0,0,0)(因为默认值floatxy的类型)而z为0)。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...