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. Dismiss Notice

Question How to Disable a Function from the same script?

Discussion in 'Scripting' started by Sushant_Dhiman, Oct 5, 2020.

  1. Sushant_Dhiman

    Sushant_Dhiman

    Joined:
    Oct 1, 2020
    Posts:
    22
    Hi, I have a script which add force to a Ball when we drag Mouse OR Touch on screen just like angry bird. But when I drag mouse on screen force is added to ball which is fine. But when we drag again even if ball is in air it force is added again. I want to add force just one time. How can i disable force if it is already added? Check my script codes.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class GameManager : MonoBehaviour
    5. {
    6.     #region Singleton class: GameManager
    7.  
    8.     public static GameManager Instance;
    9.  
    10.     void Awake ()
    11.     {
    12.         if (Instance == null) {
    13.             Instance = this;
    14.         }
    15.     }
    16.  
    17.     #endregion
    18.  
    19.     Camera cam;
    20.  
    21.     public Ball ball;
    22.     public Trajectory trajectory;
    23.     [SerializeField] float pushForce = 4f;
    24.  
    25.     bool isDragging = false;
    26.  
    27.     Vector2 startPoint;
    28.     Vector2 endPoint;
    29.     Vector2 direction;
    30.     Vector2 force;
    31.     float distance;
    32.  
    33.     //---------------------------------------
    34.     void Start ()
    35.     {
    36.         cam = Camera.main;
    37.         ball.DesactivateRb ();
    38.     }
    39.  
    40.     void Update ()
    41.     {
    42.         if (Input.GetMouseButtonDown (0)) {
    43.             isDragging = true;
    44.             OnDragStart ();
    45.         }
    46.         if (Input.GetMouseButtonUp (0)) {
    47.             isDragging = false;
    48.             OnDragEnd ();
    49.         }
    50.  
    51.         if (isDragging) {
    52.             OnDrag ();
    53.         }
    54.     }
    55.  
    56.     //-Drag--------------------------------------
    57.     void OnDragStart ()
    58.     {
    59.         ball.DesactivateRb ();
    60.         startPoint = cam.ScreenToWorldPoint (Input.mousePosition);
    61.  
    62.         trajectory.Show ();
    63.     }
    64.  
    65.     void OnDrag ()
    66.     {
    67.         endPoint = cam.ScreenToWorldPoint (Input.mousePosition);
    68.         distance = Vector2.Distance (startPoint, endPoint);
    69.         direction = (startPoint - endPoint).normalized;
    70.         force = direction * distance * pushForce;
    71.  
    72.         //just for debug
    73.         Debug.DrawLine (startPoint, endPoint);
    74.  
    75.  
    76.         trajectory.UpdateDots (ball.pos, force);
    77.     }
    78.  
    79.     void OnDragEnd ()
    80.     {
    81.         //push the ball
    82.         ball.ActivateRb ();
    83.  
    84.         ball.Push (force);
    85.  
    86.         trajectory.Hide ();
    87.  
    88.        
    89.     }
    90.  
    91. }
    92.  
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    by adding state logic.
    you can use a switching mechanism that keeps track of the last state and to prohibit dragging until a certain condition is met.

    Code (csharp):
    1. bool _draggingState = false;
    2. bool _draggingShouldWork = true; // let's say true is a default state, to enable this initially
    3.  
    4. void OnDragStart() {
    5.   if(!_draggingShouldWork) return;
    6.   _draggingShouldWork = false;
    7.   _draggingState = true;
    8.   // other stuff
    9. }
    10.  
    11. void OnDrag() {
    12.   if(!_draggingState) return;
    13.   // other stuff
    14. }
    15.  
    16. void OnDragEnd() {
    17.   if(!_draggingState) return;
    18.   _draggingState = false;
    19.   // other stuff
    20. }
    21.  
    22. // implement this on your own, this is only an illustration
    23. // you obviously need to meet certain conditions to re-enable dragging
    24. // by checking for the appropriate ball parameters in update
    25. void BallHasLandedAndIsInRest() {
    26.   _draggingShouldWork = true;
    27. }
    Read more about the finite state machines (or FSM), this is one of the simplest possible. You need them a lot to control specific compound behaviors such as this. Thankfully this is only UI, and UI state machines tend to be really simple and thus usually bug-free. On the other end of the spectrum they can be so incredibly complex that you can't approach them naively like this. Learn just the basic ropes of how to think about them, and learn about the relational and logical operators in C# so that you can write compound state evaluators with confidence.

    In general, you want to make your code guarded by some switching mechanism like this one, so that the flow is always governed with a few simple states that you can easily keep track of. Just be mindful of the life cycle of each state: its purpose, when to begin, when to end; then there are certain parallel combinations between two or more switches, one state might affect the other state, some might get canceled etc. It can become hard to memorize in some cases, so try to draw a simple diagram before implementing it.
     
    Last edited: Oct 5, 2020
  3. Sushant_Dhiman

    Sushant_Dhiman

    Joined:
    Oct 1, 2020
    Posts:
    22
    I am not able to understand can you please send me a modified code that will allow only to drag once?
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I'm sorry, that's the best I can do with your problem right now. It should be enough.
     
  5. Sushant_Dhiman

    Sushant_Dhiman

    Joined:
    Oct 1, 2020
    Posts:
    22
    Thanks buddy after doing some experiments my code is working.