- Home /
How is it possible to Lerp from a non initialized value to an initialized value?
Hi, I am confused how I can Lerp from inputDirection to targetInput when inputDirection has not yet been initialized, only defined. This piece of code is from a tutorial and I know it works, but I don't know why. My guess is that inputDirection is a special type of Vector3 that is a reference to user input or that the default value for Vector3 is (0, 0, 0), but my research so far has been unsuccessful.
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, 0, v);
inputDirection = Vector3.Lerp(inputDirection, targetInput, Time.deltaTime * 10f);
}
}
Answer by TheonlysiQ · Oct 08, 2020 at 07:59 AM
I don't see any indication that inputDirection is actually related to any user input.
Even if you don't see it there, the inputDirection variable has an init value. So i suspect (like you did) that the vector3 default init from c# sets them all to 0. To test this, you can just declare a vector3 somewhere in your code and Debug.Log() it. It will print all values and if my guess is right, it should print (0,0,0).
EDITED
Vector3 is a struct and NOT a class, C# inits it to (0,0,0).
This is partially correct, the default value for a Vector3 is (0, 0, 0), but Vector3 is not a class it is a struct. When you declare a struct it automatically allocates memory for it. The main components of the struct are floats x, y, and z, and when the space is allocated for the struct these values are auto initialized to zero because C# auto initializes its primitives. This only works because Vector3 is a struct, if it was a class it would be initialized as a null pointer.
Yes, that is true. Forgot it is a struct, even though I noticed this a while ago when i needed to do several copies of a Vector3. Either way, thank you for the correction!
Your answer
![](https://koobas.hobune.stream/wayback/20220613013338im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Is it possible to use animation State Machines for input management? 0 Answers
use animator and input on screen button 0 Answers
How to play the animation completely on mouse click 1 Answer
Creating complex Animator Controllers through script 1 Answer