Search Unity

Top down movement

Discussion in 'Scripting' started by GieskeBon, Jan 31, 2013.

  1. GieskeBon

    GieskeBon

    Joined:
    Jan 31, 2013
    Posts:
    2
    Hey guys,

    I got a question. I working on a simple game, but I'm stuck at the controlls. What I want is that the player moves exactly an amount of pixels (lets say 32 pixels in mine game). So if i press up, the player must move exactly 32 pixels up.

    here's an example of Boulderdash wich uses what I want:
    http://www.youtube.com/watch?v=FiEVfa1OK_o

    Thanks in the advance!
     
  2. GieskeBon

    GieskeBon

    Joined:
    Jan 31, 2013
    Posts:
    2
  3. piyush3dxyz

    piyush3dxyz

    Joined:
    Feb 2, 2013
    Posts:
    1
    use
    gameobject.tranform.x
     
  4. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I would set 2D grid and move the player between the points when they move the arrow keys rather than have a character controller.
     
  5. Kaze_Senshi

    Kaze_Senshi

    Joined:
    Feb 19, 2012
    Posts:
    243
    When pressing a key you can make the player enter in a state where he ignores all inputs and moves in one direction in a specific time. I'll use units in the example because I don't know how to control the movement by pixels.

    Code (csharp):
    1.  
    2. function Update()
    3. {
    4. if( walkToLeft == true)
    5. {
    6.   // TIME CHECK
    7.   if( keyTime + 1.0 > Time.time ) // 32 units per second, each movement takes 1 second
    8.   {
    9.        transformToMove.Translate( Vector3( -32, 0, 0) * Time.deltaTime, Space.World ); // Move function
    10.   }
    11.  else // Time over, end of the movement
    12.  { walkToLeft = false; }
    13. }
    14. else if( Input.GetButton("Left") ) // If the player isn't walking, you can make it walk
    15. {
    16.  walkToLeft = true;
    17.  keyTime = Time.time; // To control the time
    18. }
    19.  
    20.  
     
  6. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    the movement based on pixels will also depend on where the camera is placed.

    Instead of using pixels use x and z or something.