Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lucidcloud · Oct 08, 2020 at 02:27 AM · inputanimator controlleranimation script

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);
  }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_ek98vnTRplGj8Q · Oct 08, 2020 at 03:18 PM 2
Share

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.

avatar image TheonlysiQ unity_ek98vnTRplGj8Q · Oct 08, 2020 at 05:30 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges