Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

conditional if help

Discussion in 'Scripting' started by vmp, Dec 11, 2010.

  1. vmp

    vmp

    Joined:
    Mar 13, 2006
    Posts:
    23
    i am just building up a simple controller for a 2.5d sidescroller, but i have been having issues with the following script

    i am storing the horizontal key press in a variable
    then using floortoInt to make it a whole number which is either -1,0 or 1 depending on wether left or right key or neither key was pressed and storing it in a var curDir

    then i go into an if else loop - if curDir is not equal to lasDir - but this is where its not working for me correctly its still going into the loop even tho curDir and lasDir are equal

    if you add this script to a gameobject with a character controller you will see i am putting the variable in question to a gui text i have also added an inUse variable that just ticks over when ever we are in one of the loops

    the strange thing is if you press the "d" key to go right then let it go everything is as should be but then if you press the "a" key to go left and let it go the inUse var keeps ticking over

    i am sure its something i am doing wrong



    Code (csharp):
    1. var curDir : int = 1;
    2. var lasDir : int = 1;
    3. var InUse =0;
    4.  
    5. private var horizDir : float = 0;
    6. private var thisTransform : Transform;
    7. private var character : CharacterController;
    8.  
    9. function Start()
    10. {
    11.     // Cache component lookup at startup instead of doing this every frame     
    12.     thisTransform = GetComponent( Transform );
    13.     character = GetComponent( CharacterController );   
    14. }
    15.  
    16. function OnEndGame()
    17. {
    18.     // Don't allow any more control changes when the game ends
    19.     this.enabled = false;
    20. }
    21.  
    22. function Update()
    23. {
    24.     charDirection();
    25.  
    26. }
    27.  
    28. function charDirection()
    29. {
    30.  
    31.     horizDir = Input.GetAxis("Horizontal");
    32.     curDir = Mathf.FloorToInt(horizDir); //round key press to whole number
    33. if (curDir != lasDir)
    34. {
    35.     if (horizDir > 0) // going right
    36.         {
    37.         transform.rotation = Quaternion.LookRotation(Vector3.forward);
    38.         lasDir = curDir;
    39.         InUse ++;
    40.         }  
    41.     else if (horizDir <0) // going left
    42.         {
    43.         transform.rotation = Quaternion.LookRotation(-Vector3.forward);
    44.         lasDir = curDir;
    45.         InUse ++;
    46.         }
    47.     else // last direction
    48.         {
    49.     InUse ++;
    50.         }
    51.  
    52. }
    53.  
    54. }
    55.    
    56.    
    57.     function OnGUI()
    58. {
    59.   GUI.Label(Rect(0,0,200,50),GUIContent ("curDir :"+curDir));
    60.   GUI.Label(Rect(0,50,200,50),GUIContent ("lasDirs :"+curDir));
    61.   GUI.Label(Rect(0,100,200,100),GUIContent ("processTest :"+InUse));
    62.  
    63. }
     
    Last edited: Dec 11, 2010
  2. Ostagar

    Ostagar

    Joined:
    Sep 29, 2010
    Posts:
    445
    In the else block, you forgot to set lastDir = curDir. That means if you go in some direction (say curDir=1) and then stop (curDir=0), InUse will continue to increment indefinitely (which is what you say you don't want).
     
    Last edited: Dec 11, 2010
  3. vmp

    vmp

    Joined:
    Mar 13, 2006
    Posts:
    23
    Thank you
    so simple i could kick myself

    Arthur
     
  4. Ostagar

    Ostagar

    Joined:
    Sep 29, 2010
    Posts:
    445
    np. That often happens to me when working late. ;)
     
unityunity