Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How does unity know that is the Y

Discussion in 'Scripting' started by LibroVecchio, Aug 4, 2023.

  1. LibroVecchio

    LibroVecchio

    Joined:
    Aug 1, 2023
    Posts:
    1
    I picked up unity just a few days ago and started reading a book called "Unity in action" (Manning, 2022).
    The book does this to add gravity to the player so it doesn't float, but there's a thing I dont understand: why does he write "movement.y" to add gravity? How does unity know that "y" is the second position in the vector? The book does not explain it, sadly, and I miss a piece.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [RequireComponent(typeof(CharacterController))]
    5. [AddComponentMenu("Control Script/FPS Input")]
    6. public class FPSInput : MonoBehaviour {
    7. public float speed = 6.0f;
    8. public float gravity = -9.8f;
    9. private CharacterController charController;
    10. void Start() {
    11. charController = GetComponent<CharacterController>();
    12. }
    13. void Update() {
    14. float deltaX = Input.GetAxis("Horizontal") * speed;
    15. float deltaZ = Input.GetAxis("Vertical") * speed;
    16. Vector3 movement = new Vector3(deltaX, 0, deltaZ);
    17. movement = Vector3.ClampMagnitude(movement, speed);
    18. movement.y = gravity;
    19. movement *= Time.deltaTime;
    20. movement = transform.TransformDirection(movement);
    21. charController.Move(movement);
    22. }
    23. }
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,084
    Simple answer: Unity "knows" because it's a type that belongs to Unity.

    Complex answer: Unity "knows" because the DLL that it resides within has meta information informing the compiler of the layout of its fields (X, Y, and Z in the case of Vector3) and methods (eg ClampMagnitude).

    https://docs.unity3d.com/ScriptReference/Vector3.html

    Just in case you're not aware this book assumes you already know C#. If you haven't taken the time to properly learn it you might find it difficult to understand and follow along with that book as it becomes more complex.
     
    Last edited: Aug 4, 2023
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    In C#, a variable named
    movement
    is declared here as
    Vector3
    , and a new instance of this type is assigned when you see
    new Vector3( ... )
    .

    Vector3 is a type that comes with Unity (namely in UnityEngine namespace) that is defined as having three public fields named
    x
    y
    and
    z
    (among other things).

    To access public members of this data instance in C#, a dot (
    .
    ) operator is used. Hence
    movement.y
    .

    This particular type is known as a value type, because it's a struct. This code manipulates the x,y,z set of simple floating point values by passing the whole thing around, and usually getting a fresh instance out, after some function has been applied. Because it's a value type, producing a new instance doesn't allocate memory on the heap, which makes it more lightweight than an ordinary object, however it is passed around by value, not by reference. This is because of how C# is designed to work with such data.

    Regardless, Vector3 type is ubiquitous in Unity because it is used to describe and allow easy work with 3D vectors, points, and directions, and you can find examples of it in almost every publicly available snippet of code.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,913
    Books like those are generally aiming to give practical advice for what to type to make Unity do things. That part is telling you that in Unity;s xyz coordinate system, y is up and down and you can set&read them with whatever.x, whatever.y and whatever-dot-z. It purposely doesn't explain why, since it would take a long time and most people reading a book like that don't want to know.

    It you want to know how it works -- or if you want to be able to write original Unity scripts or even make big changes to existing ones -- you'll need to read a book about plain old computer programming. Because Unity has special added commands for changing colors, making shapes and so on, but the rules for using them and having the game decide when and where, are regular old programming.
     
  5. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    In your code, movement is the character controller's local movement vector. You can imagine it by pointing an arrow forward from your own belly, and deciding that you will move to the direction of this vector. X means left/right, Z means forward/back, Y means up/down.

    So, first the vector is initialized to be
    (deltaX, 0, deltaZ)
    , which means that you're going to go left/right and forward/back according to what your movement keys are giving to you, multiplied by speed. Y is zero because you're not going to move up or down.

    Then, gravity is added to your local movement vector (this could be also added to your global movement vector but here it's done this way).

    Now you have a local movement vector that is going left/right, forward/back according to movement keys and speed, and gravity which takes you down with a static amount per frame.

    Next, this local movement vector is transformed into a global movement vector by calling
    transform.TransformDirection(movement)
    on your character's transform. Note that
    transform
    here means
    MonoBehaviour.transform
    , meaning that it is the transform of the GameObject that this script is attached to. This translates from your character's local coordinate system (your belly) to the global coordinate system of your scene. Then, the character transform is moved using this global vector.
     
    Last edited: Aug 5, 2023