Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

If statement doesn't seem to be working.

Discussion in 'Scripting' started by LucioHernandez, Apr 27, 2022.

  1. LucioHernandez

    LucioHernandez

    Joined:
    Apr 21, 2022
    Posts:
    10
    So, this is the code of a platform that can move around and rotate, but when it touches the player it freezes in place, but even if the movePlat variable is set as true, the platform still moves, I can't really pinpoint what's wrong with the code so any help I can get is greatly appreciate.
    Code (CSharp):
    1. public class RotateWithKeyboard : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField] private float speed = .1f;
    5.     Vector2 lastClickedPos;
    6.     public float followSpeed = 10f;
    7.     bool moving;
    8.     bool movePlat = false;
    9.  
    10.     private BoxCollider2D coll;
    11.     private void Start()
    12.     {
    13.         coll = GetComponent<BoxCollider2D>();
    14.     }
    15.  
    16.     private void OnCollisionEnter2D(Collision2D collision)
    17.     {
    18.         Debug.Log("Collided");
    19.         if (collision.transform.gameObject.name == "Player")
    20.         {
    21.             Debug.Log("Collided with player");
    22.             bool movePlat = true;
    23.             Debug.Log(movePlat);
    24.         }
    25.        
    26.     }
    27.     private void OnCollisionExit2D(Collision2D collision)
    28.     {
    29.         Debug.Log("Player Left Platform");
    30.         bool movePlat = false;
    31.         Debug.Log(movePlat);
    32.  
    33.     }
    34.  
    35.     private void Update()
    36.     {
    37.         if (movePlat == false){
    38.  
    39.             if (Input.GetKey(KeyCode.Q))
    40.             {
    41.                 transform.Rotate(0, 0, -360 * speed * Time.deltaTime);
    42.             }
    43.             else if (Input.GetKey(KeyCode.E))
    44.                 transform.Rotate(0, 0, 360 * speed * Time.deltaTime);
    45.  
    46.             if (Input.GetMouseButtonDown(0))
    47.             {
    48.                 lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    49.                 moving = true;
    50.             }
    51.             if (moving && (Vector2)transform.position != lastClickedPos)
    52.             {
    53.                 float step = followSpeed * Time.deltaTime;
    54.                 transform.position = Vector2.MoveTowards(transform.position, lastClickedPos, step);
    55.             }
    56.             else
    57.             {
    58.                 moving = false;
    59.             }
    60.         }
    61.  
    62.         if(movePlat == true)
    63.         {
    64.             if (Input.GetKey(KeyCode.Q))
    65.             {
    66.                 transform.Rotate(0, 0, -360 * 0 * Time.deltaTime);
    67.             }
    68.             else if (Input.GetKey(KeyCode.E))
    69.                 transform.Rotate(0, 0, 360 * 0 * Time.deltaTime);
    70.  
    71.             if (Input.GetMouseButtonDown(0))
    72.             {
    73.                 lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    74.                 moving = true;
    75.             }
    76.             if (moving && (Vector2)transform.position != lastClickedPos)
    77.             {
    78.                 float step = followSpeed * 0;
    79.                 transform.position = Vector2.MoveTowards(transform.position, lastClickedPos, step);
    80.             }
    81.             else
    82.             {
    83.                 moving = false;
    84.             }
    85.         }
    86.  
    87.        
    88.     }
    89. }
     
  2. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    404
    Code (CSharp):
    1.         bool movePlat = true;
    You're redefining a second 'movePlat' inside your collision handlers that only exists within the scope of that function.
     
    LucioHernandez likes this.
  3. LucioHernandez

    LucioHernandez

    Joined:
    Apr 21, 2022
    Posts:
    10
    Wait oh my god I'm so dumb, thank you so much man you're a life saver <3