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

Random 2D Movement

Discussion in 'Scripting' started by Spoxx, Feb 19, 2019.

  1. Spoxx

    Spoxx

    Joined:
    Feb 19, 2019
    Posts:
    8
    Hello does anybody here know how to make my player move in a random direction but not by itself
    I want it to move randomly when i press a key ,so when i press UpArrow for example i want it to either move
    UP or DOWN or LEFT or RIGHT so for each of the four keys there are 4 direction possibilities.

    Here is the code that i use now for the player to move

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class playercontrol : MonoBehaviour
    5. {
    6.  
    7.     Vector2 h;
    8.  
    9.     void Update()
    10.     {
    11.  
    12.         if (Input.GetKeyDown(KeyCode.W))
    13.         {
    14.             h = new Vector2(transform.position.x, transform.position.y + 30 * Time.deltaTime);
    15.             transform.position = h;
    16.  
    17.  
    18.         }
    19.         if (Input.GetKeyDown(KeyCode.A))
    20.         {
    21.            
    22.             h = new Vector2(transform.position.x - 30 * Time.deltaTime, transform.position.y);
    23.             transform.position = h;
    24.  
    25.         }
    26.         if (Input.GetKeyDown(KeyCode.S))
    27.         {
    28.             h = new Vector2(transform.position.x, transform.position.y - 30 * Time.deltaTime);
    29.             transform.position = h;
    30.  
    31.         }
    32.         if (Input.GetKeyDown(KeyCode.D))
    33.         {
    34.             h = new Vector2(transform.position.x + 30 * Time.deltaTime, transform.position.y);
    35.             transform.position = h;
    36.  
    37.         }
    38.        
    39.      
    40.        
    41.     }
    42.  
    43. }
    44.  
    45.  
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Well, that's easy. Rotate your vector 0, 90, 180 or 270 degrees before assigning to position.
     
  3. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    338
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. public class playercontrol : MonoBehaviour
    4. {
    5.  
    6.     Vector2 h;
    7.  
    8.     void Update()
    9.     {
    10.  
    11.         if (Input.GetKeyDown(KeyCode.W))
    12.         {
    13.  
    14.             MoveToAnyDirection();
    15.  
    16.         }
    17.         if (Input.GetKeyDown(KeyCode.A))
    18.         {
    19.  
    20.             MoveToAnyDirection();
    21.  
    22.         }
    23.         if (Input.GetKeyDown(KeyCode.S))
    24.         {
    25.             MoveToAnyDirection();
    26.  
    27.         }
    28.         if (Input.GetKeyDown(KeyCode.D))
    29.         {
    30.  
    31.             MoveToAnyDirection();
    32.         }
    33.  
    34.  
    35.  
    36.     }
    37.  
    38.     private void MoveToAnyDirection()
    39.     {
    40.         int random = UnityEngine.Random.Range(1, 5);
    41.         if(random == 1)
    42.         {
    43.             h = new Vector2(transform.position.x, transform.position.y + 30 * Time.deltaTime);
    44.            
    45.         }
    46.         else if(random == 2)
    47.         {
    48.             h = new Vector2(transform.position.x - 30 * Time.deltaTime, transform.position.y);
    49.         }
    50.         else if(random == 3)
    51.         {
    52.             h = new Vector2(transform.position.x, transform.position.y - 30 * Time.deltaTime);
    53.         }
    54.         else if(random == 4)
    55.         {
    56.             h = new Vector2(transform.position.x + 30 * Time.deltaTime, transform.position.y);
    57.         }
    58.         transform.position = h;
    59.     }
    60. }
     
    Spoxx likes this.
  4. Spoxx

    Spoxx

    Joined:
    Feb 19, 2019
    Posts:
    8
    Thank you very much it works perfectly.
     
  5. Spoxx

    Spoxx

    Joined:
    Feb 19, 2019
    Posts:
    8
    I am sorry but i am new to Unity and i dont understand what you mean, i have the solution now thanks to FernandoHC but i would still like to know your solution too if it isnt in the same method so if you could explain in more detail i would appreciate it.Thank you
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    In your code you assign a new vector for every different key. But if you want to move in random direction, you may assign one random vector when any of keys is pressed, lile this:

    Code (CSharp):
    1.         private readonly Vector3[] directions = {Vector3.left, Vector3.forward, Vector3.right, Vector3.back};
    2.      
    3.         private void Update() {
    4.             if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D)) {
    5.                 var randomDirection = Random.Range(0, directions.Length);
    6.                 transform.position += Time.deltaTime * directions[randomDirection];
    7.             }
    8.         }
     
    Last edited: Feb 20, 2019
    Spoxx and FernandoHC like this.