Search Unity

Stop moving in one direction

Discussion in 'Scripting' started by Peree, Feb 17, 2020.

  1. Peree

    Peree

    Joined:
    Feb 17, 2020
    Posts:
    3
    so i started learning unity and first thing i got in mind that's not so hard is to make something like "brick breaker" kind of game, all went good till i started to fix some bugs. The one that i need help with is that a platform will stop moving if i got out of screen, i got idea that i need to make borders and said to script if it touch border then stop moving in that direction butt that was something i know i can do so i wanted to do something harder like this: when platform get to right x coordinates then it stop moving, and my unity crash every time i run the game. So i waned to know if that's even possible or i just lose 5 hours of my life ;D
    sorry for bad English and here is my code

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Player1 : MonoBehaviour
    7. {
    8.     public float speed;
    9.     private bool border1 = true;
    10.     private bool border2 = true;
    11.     void Update()
    12.     {
    13.         try {
    14.  
    15.         if (border1 == false)
    16.             {
    17.                 speed = 0;
    18.             }
    19.         if (border2 == false)
    20.             {
    21.                 speed = 0;
    22.             }
    23.  
    24.             if (Input.GetKey(KeyCode.LeftArrow))
    25.         {
    26.  
    27.                 transform.position += Vector3.left * speed * Time.deltaTime;
    28.  
    29.         }
    30.  
    31.         if (Input.GetKey(KeyCode.RightArrow))
    32.         {
    33.  
    34.                 transform.position += Vector3.right * speed * Time.deltaTime;
    35.  
    36.         }
    37.  
    38.  
    39.         while (transform.position.x > 9.5)
    40.         {
    41.  
    42.                 border1 = false;
    43.  
    44.         }
    45.  
    46.         while (transform.position.x < -9.5)
    47.         {
    48.  
    49.                 border2 = false;
    50.  
    51.         }
    52.  
    53.         }
    54.  
    55.         catch
    56.        
    57.         {
    58.             Debug.Log("sjebo se");
    59.         }
    60.  
    61.         }
    62.  
    63.     }
    64.  
    65.  
     
  2. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    129
    While declares a loop but what you really want to do is check that condition once only. And by the looks of it you already know the right keyword for that so with that little bit of advice, I think you can already work it out by yourself. ;)
     
    Peree likes this.
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    in other words, 'while' might look similar to 'if', but in reality it is much more similar to 'for' loop.
    however, unlike 'for' it's a more general syntax, that doesn't use a counter.

    'if' is a single, one-time condition check that evaluates the expression in the parentheses and moves on with life.

    'while' is typically used as a code block that will repeat until the expression is evaluated as true false (edit: slip of the tongue), meaning it will suspend all activity, including unity moving forward in time, advancing to the next frame, and so on. if there is nothing going on with it, it will practically freeze everything in place, as it cannot resolve gracefully and move on.

    also it doesn't make sense to use 'try' if you don't know what it's for. OR, maybe you do, but 'while' freezing won't produce an error, at least not in a technical sense. it just hangs unity indefinitely.

    btw I'm a native speaker of the language in which that Debug.Log speaks ;)
     
    Last edited: Feb 18, 2020
    Peree likes this.
  4. Peree

    Peree

    Joined:
    Feb 17, 2020
    Posts:
    3
    HAHAHAHAHHA i so over thinked this, thanks guys
     
  5. Peree

    Peree

    Joined:
    Feb 17, 2020
    Posts:
    3
    here is code that fix this problem, i just made him tp by small amount when he go out
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player1 : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public GameObject gameObjectToMove;
    9.     void Update()
    10.     {
    11.  
    12.         if (transform.position.x <= -9.52f)
    13.         {
    14.             gameObjectToMove.transform.position = new Vector3(-9.45f, -4, 1);
    15.         }
    16.        
    17.         if (transform.position.x >= 9.52f)
    18.         {
    19.             gameObjectToMove.transform.position = new Vector3(9.45f, -4, 1);
    20.         }
    21.  
    22.         if (Input.GetKey(KeyCode.LeftArrow))
    23.         {
    24.             transform.position += Vector3.left * speed * Time.deltaTime;
    25.         }
    26.  
    27.         if (Input.GetKey(KeyCode.RightArrow))
    28.         {
    29.             transform.position += Vector3.right * speed * Time.deltaTime;
    30.         }
    31.     }
    32. }
    33.