Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can someone explain transform.position and Vector?

Discussion in 'Scripting' started by ramenman115, Feb 10, 2016.

  1. ramenman115

    ramenman115

    Joined:
    Feb 9, 2016
    Posts:
    1
    Hello.

    Im trying to wrap my head around the basics of Unity and C# programming. Im trying to understand how Vector and transform.position work. Currently I'm working on the Ball Game tutorial and stopped at this part to understand it better.

    publicGameObjectPlayer; //ReferencetothePlayerSphereGameObject.

    privateVector3offset; //Itsprivatebecausewecansetthevaluehere.

    voidStart () {

    offset = transform.position - Player.transform.position;

    }

    voidLateUpdate() {

    transform.position = Player.transform.position - offset;
    }

    Thank You!
     
  2. Eths

    Eths

    Joined:
    Mar 5, 2015
    Posts:
    184
    Hello ramenman115, welcome to unity, I don't know how to explain how vector3 and transform.position works but I will try to explain as possible.

    Vector3 is a structure that Describes a position in three-dimensional (3-D) space.

    for example:

    Code (CSharp):
    1. Vector3 Myvector = new Vector3(15,6,2); // we create a vector3 with name or variable "Myvector" that represents the point 15 at x-axis, and point 6 at y axis and point 2 at z axis.
    so Vector3 is somewhat a variable that is used to represent a specific point in your game.

    where the first variable (which is 15 in the above code) represent the "x axis" ,the second variable (which is 6 in the above code) represent the "y axis", the third variable (which is 2 in the above code) represent the "z axis".

    now transform.position is simple. transform.position is used to get the Vector3 of the current game object.

    for example, if you have gameobject called "Player" we can use this code to get it's Vector3 (position inside the game)


    Code (CSharp):
    1. public GameObject Player;
    2. public Vector3 PlayerPosition;
    3.  
    4. void Start()
    5. {
    6.    PlayerPosition = PlayerObject.transform.position;
    7. }
    if you tried to Debug.Log "PlayerPosition" 3 variables will be shown in console window.

    So to summarize the above.
    • Vector3 is a three-dimensional (3-D) variable which represent a point inside the game.
    • transform.position is a function, that we use to get the Vector3 (point inside the game) of the player.
     
  3. Mafela

    Mafela

    Joined:
    Jun 26, 2022
    Posts:
    1
    If you want to move your ball try this in your update function



    Player class{

    float speed 2;
    Float horizontal;
    Float vertical;
    Rigitbody rb;

    Void start{
    rb = get.component<rigitbody>
    }
    Void update {
    horizontal = get.axis.raw( "horizontal")
    vertical = get.axis.raw("vertical)

    If (input.get.button.down(keycode.space))

    rb.velocity = vector3(horizontal * speed,0f ,vertical * speed);



    }
     
  4. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    113
    Bonjour ramenman115 ,

    can you use the code tag on the forum it is easier to read and help.
    Code (CSharp):
    1. public GameObject Player; //ReferencetothePlayerSphereGameObject.
    2.  
    3. private Vector3 offset; //Itsprivatebecausewecansetthevaluehere.
    4.  
    5. voidStart () {
    6.  
    7. offset = transform.position - Player.transform.position;
    8.  
    9. }
    10.  
    11. voidLateUpdate() {
    12.  
    13. transform.position = Player.transform.position - offset;
    14. }

    transform.position :

    Represents the coordinates of an object. It's in the transform component of the inspector (RED), and it's the position parameter that's changed (YELLOW). (Open picture)



    Vector3 :
    is a variable that stores 3 values.
    Here we use it to change the 3 values of POSITION , so it should be understood like this :
    Vector3(X,Y,Z);

    The first value will change the X coordinate, ...
     

    Attached Files:

    • help.png
      help.png
      File size:
      13.3 KB
      Views:
      183
    Last edited: Jun 26, 2022
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,954
    Reviving a six year old thread with your first post isn't a terribly good idea either.
     
    bplc likes this.
  6. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    113
    Well seen Ryiah.
    I did not notice.