Search Unity

Resolved Making 2d player restricted to walking across certain gameobjects

Discussion in 'Scripting' started by ArthurHaleta, May 21, 2022.

  1. ArthurHaleta

    ArthurHaleta

    Joined:
    Dec 24, 2021
    Posts:
    44
    This proved quite difficult for me, but I have made an attempt:
    Code (CSharp):
    1. public static string lastMove;
    2.     public static bool grounded = true;
    3.  
    4.     void OnTriggerEnter2D(Collider2D col)
    5.     {
    6.         if(col.transform.name == "plat")
    7.         {
    8.             grounded = true;
    9.         }
    10.     }
    11.  
    12.     void OnTriggerExit2D(Collider2D col)
    13.     {
    14.         grounded = false;
    15.         /*
    16.         if (col.transform.name == "plat")
    17.         {
    18.             if (lastMove == "u")
    19.             {
    20.                 Move("d");
    21.             }
    22.             else if (lastMove == "d")
    23.             {
    24.                 Move("u");
    25.             }
    26.             else if (lastMove == "l")
    27.             {
    28.                 Move("r");
    29.             }
    30.             else if (lastMove == "r")
    31.             {
    32.                 Move("l");
    33.             }
    34.         }
    35.         */
    36.     }
    37.  
    38.     public static void Move(string dir)
    39.     {
    40.         lastMove = dir;
    41.  
    42.         if (dir == "u")
    43.         {
    44.             GameObject.Find("plr").transform.Translate(0, 1f, 0);
    45.  
    46.             if (!grounded)
    47.             {
    48.                 GameObject.Find("plr").transform.Translate(0, -1f, 0);
    49.                 grounded = true;
    50.             }
    51.         }
    52.         else if (dir == "d")
    53.         {
    54.             GameObject.Find("plr").transform.Translate(0, -1f, 0);
    55.  
    56.             if (!grounded)
    57.             {
    58.                 GameObject.Find("plr").transform.Translate(0, 1f, 0);
    59.                 grounded = true;
    60.             }
    61.         }
    62.         else if (dir == "l")
    63.         {
    64.             GameObject.Find("plr").transform.Translate(-1f, 0, 0);
    65.  
    66.             if (!grounded)
    67.             {
    68.                 GameObject.Find("plr").transform.Translate(1f, 0, 0);
    69.                 grounded = true;
    70.             }
    71.         }
    72.         else //r
    73.         {
    74.             GameObject.Find("plr").transform.Translate(1f, 0, 0);
    75.  
    76.             if (!grounded)
    77.             {
    78.                 GameObject.Find("plr").transform.Translate(-1f, 0, 0);
    79.                 grounded = true;
    80.             }
    81.         }
    82.     }
    Moving works, but not the whole grounded thing.
    upload_2022-5-21_12-0-9.png
    So I want the yellow cube to be only allowed to walk on the platform, so when the player moves off, I move them back. But instead, it stops walking off once, then when you click again, you move off. I would love someone to either help fix this, or provide an easier way of doing this. Thank you! :D
     
  2. ArthurHaleta

    ArthurHaleta

    Joined:
    Dec 24, 2021
    Posts:
    44
    Okay, I figures something out. First, I got the platform the player is on as a collider:
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D col)
    2.     {
    3.         if(col.transform.CompareTag("blk"))
    4.         {
    5.             currentPlat = col;
    6.         }
    7.     }
    Then everytime the player wanted to move, I asked if currentPlat (the currentPlatform) contained the players position + where they want to move, so:
    Code (CSharp):
    1. if (currentPlat.bounds.Contains(new Vector2(GameObject.Find("plr").transform.position.x, GameObject.Find("plr").transform.position.y + 1)))
    2.             {
    3.                 GameObject.Find("plr").transform.Translate(0, 1f, 0);
    4.             }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
  4. ArthurHaleta

    ArthurHaleta

    Joined:
    Dec 24, 2021
    Posts:
    44
    Wow. Thank you so much; I just read the document. Very helpful. So basically, if I just assigned player to a public variable, instead of getting it every time, it would speed it up a lot? Strange, because I conditioned myself to avoid public variables, and try to get all variables needed, inside of the code. Thanks again! :D