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): var curDir : int = 1; var lasDir : int = 1; var InUse =0; private var horizDir : float = 0; private var thisTransform : Transform; private var character : CharacterController; function Start() { // Cache component lookup at startup instead of doing this every frame thisTransform = GetComponent( Transform ); character = GetComponent( CharacterController ); } function OnEndGame() { // Don't allow any more control changes when the game ends this.enabled = false; } function Update() { charDirection(); } function charDirection() { horizDir = Input.GetAxis("Horizontal"); curDir = Mathf.FloorToInt(horizDir); //round key press to whole number if (curDir != lasDir) { if (horizDir > 0) // going right { transform.rotation = Quaternion.LookRotation(Vector3.forward); lasDir = curDir; InUse ++; } else if (horizDir <0) // going left { transform.rotation = Quaternion.LookRotation(-Vector3.forward); lasDir = curDir; InUse ++; } else // last direction { InUse ++; } } } function OnGUI() { GUI.Label(Rect(0,0,200,50),GUIContent ("curDir :"+curDir)); GUI.Label(Rect(0,50,200,50),GUIContent ("lasDirs :"+curDir)); GUI.Label(Rect(0,100,200,100),GUIContent ("processTest :"+InUse)); }
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).