Search Unity

Left and right enemy moving in 2D platformer

Discussion in 'Scripting' started by Like-A-Sir, Oct 29, 2015.

  1. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33
    Good day!

    I am working on a script for an enemy in a platformer game.I want it to move a specific distance left or right and then return to the origin and just to do the same thing.
    please show me a way to script this thing.I already tried but can't figure out the conditions.(In my script I used 2 bool movingRight and movingLeft).

    Thank you very much!
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    There's loads of ways of doing something like that. A simple way would be to have 2 variables that define the minimum X position of the enemy and the maximum X position. You could also have an integer that is -1 when moving left and +1 when moving right.

    e.g.
    Code (CSharp):
    1. float fMinX = 50.0f;
    2. float fMaxX = 250.0f;
    3. int Direction = -1;
    4.  
    5. Update:
    6.  
    7. switch( Direction )
    8. {
    9.     case -1:
    10.         // Moving Left
    11.         if( fEnemyX > fMinX )
    12.         {
    13.             fEnemyX -= 1.0f;
    14.         }
    15.         else
    16.         {
    17.             // Hit left boundary, change direction
    18.             Direction = 1;
    19.         }
    20.         break;
    21.      
    22.     case 1:
    23.         // Moving Right
    24.         if( fEnemyX < fMaxX )
    25.         {
    26.             fEnemyX += 1.0f;
    27.         }
    28.         else
    29.         {
    30.             // Hit right boundary, change direction
    31.             Direction = -1;
    32.         }
    33.         break;
    34. }
    35.  
    36. sprEnemy.transform.localPosition = new Vector3( fEnemyX , 0.0f , 0.0f );
    37.  
     
    Like-A-Sir likes this.
  3. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33
    Thank you very much sir!

    You saved my day! I really appreciate your help.Here is my final code:

    Code (CSharp):
    1. // Use this for initialization
    2.     void Start ()
    3.     {
    4.         initialPosition = transform.position;
    5.         direction = -1;
    6.         maxDist += transform.position.x;
    7.         minDist -= transform.position.x;
    8.     }
    9.    
    10.     // Update is called once per frame
    11.     void Update ()
    12.     {
    13.         switch (direction)
    14.         {
    15.              case -1:
    16.                 // Moving Left
    17.                 if( transform.position.x > minDist)
    18.                     {
    19.                        GetComponent <Rigidbody2D>().velocity = new Vector2(-movingSpeed,GetComponent<Rigidbody2D>().velocity.y);
    20.                     }
    21.                 else
    22.                     {
    23.                        direction = 1;
    24.                     }
    25.                 break;
    26.              case 1:
    27.                   //Moving Right
    28.                 if(transform.position.x < maxDist)
    29.                     {
    30.                         GetComponent <Rigidbody2D>().velocity = new Vector2(movingSpeed,GetComponent<Rigidbody2D>().velocity.y);
    31.                     }
    32.                 else
    33.                     {
    34.                         direction = -1;
    35.                     }
    36.             break;
    37.         }
    38.     }
    39.  
    40. }

    Have a nice day!
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    No problem, glad you got it working ok