Search Unity

keep track of player position

Discussion in 'Scripting' started by ariusdb222, Jan 31, 2015.

  1. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    new to scripting.
    basically i want x and y to reach 2000 then become 0 again(ends my while loop).
    but i have no idea how transform and position works...
    how can i initialize x and z to 0 then count as the player moves in x and y to 2000?

    pls help..
     
    Last edited: Jan 31, 2015
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Well, x wouldn't be the character's true x position, so you would need a relative amount.
    startX = Player.Transform.position.x;
    startY = Player.Transform.position.y;

    Now you can send them back to that position.
    distX = Player.Transform.position.x - startX;
     
  3. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    ok that makes sense but how would i count that to 2000
     
  4. DryTear

    DryTear

    Joined:
    Nov 30, 2012
    Posts:
    312
    Code (csharp):
    1.  
    2. int x = 0;
    3.     int z = 0;
    4.     int y = 0;
    5.  
    6.     Vector3 lastPos;
    7.  
    8.     void Update()
    9.     {
    10.         if(transform.position.x > lastPos.x)
    11.         {
    12.             x += 1;
    13.             lastPos.x = transform.position.x;
    14.             if (x == 2000)
    15.             {
    16.                 x = 0;
    17.             }
    18.         }
    19.         if (transform.position.y > lastPos.y)
    20.         {
    21.             y += 1;
    22.             lastPos.y = transform.position.y;
    23.             if (y == 2000)
    24.             {
    25.                 y = 0;
    26.             }
    27.         }
    28.         if (transform.position.y > lastPos.z)
    29.         {
    30.             z += 1;
    31.             lastPos.z = transform.position.z;
    32.             if(z == 2000)
    33.             {
    34.                 z = 0;
    35.             }
    36.         }
    37.     }
    38.  
    Simple code.
     
  5. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    thanks a lot! made my day:)