Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I need help debugging this code.

Discussion in 'Scripting' started by CgRobot, Mar 6, 2012.

  1. CgRobot

    CgRobot

    Joined:
    Jan 4, 2011
    Posts:
    175
    Alright. I CANNOT figure out why this is not working. I have no errors and everything seems to work except this. The variable "acceleration" is supposed to increment each time the function is called (which is every frame) by the value "accel" passed into the function. In my debugging, the logs say that acceleration stay at exactly 0.1 every frame (0.1 is the number passed to accel). I tried isolating acceleration from accel because I thought that maybe every time the function is called it was being reset. However, there is still no change. I'm hoping it's something simple I'm just not seeing. Thanks for any help. Here is the code:

    Code (csharp):
    1.  
    2. function MoveToTarget(coordinates : Vector3, accel : float, accuracy : float, reset : boolean)
    3. {
    4.     var distanceToCoordinates : float;
    5.     var firstCall : boolean = false;
    6.     var acceleration : float;
    7.    
    8.  
    9.     //If this is the first time that this function was called
    10.     //This is needed to make sure we don't reset each variable
    11.     //Every time it's called
    12.     if(!firstCall)
    13.     {
    14.         firstCall = true;
    15.         acceleration = accel;
    16.     }
    17.    
    18.     //Calculates the distance to the given coordinates
    19.     distanceToCoordinates = Vector3.Distance(transform.position, coordinates);
    20.     DebugLog("MissileSpeeder/MoveToTarget/distanceToCoordinates: " + distanceToCoordinates);
    21.     DebugLog("MissileSpeeder/MoveToTarget/acceleration: " + acceleration);
    22.     DebugLog("MissileSpeeder/MoveToTarget/firstCall: " + firstCall);
    23.  
    24.     if(!reset)
    25.     {
    26.         //Rotates the vehicle toward coordinates
    27.         targetRotation = Quaternion.LookRotation (coordinates - transform.position);
    28.         transform.localRotation = Quaternion.Lerp (transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
    29.  
    30.         //Moves the vehicle toward the coordinates
    31.         transform.Translate(Vector3.forward * (Time.deltaTime + acceleration));
    32.  
    33.         acceleration += 0.1;
    34.  
    35.     }else//Reset all variables back to default for next call
    36.     {
    37.         acceleration = 0;
    38.         firstCall = false;
    39.     }
    40. }
    41.  
     
  2. CgRobot

    CgRobot

    Joined:
    Jan 4, 2011
    Posts:
    175
    Ok. I've found the problem but I don't know how to fix it. The problem is in line 4. Every time the function is called it resets "firstCall" to false which means that the bit of code in lines 11-15 that is supposed to prevent the values from being reset each time is executing on each call. So I'm back to square one. So here's my new question. When ever this function is called, more than likely it will be called repeatedly for several frames. All I'm trying to do is pass it several parameters on the first call, but prevent them from being reset on any of the following calls until I choose to reset them. How could I do this?
     
  3. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    Without studying the code too closely, I would say move "firstCall" variable outside of the function so that it becomes a member variable of the class.
     
  4. Jacob-Aldridge

    Jacob-Aldridge

    Joined:
    Feb 26, 2009
    Posts:
    120
    You need to make the acceleration variable a member of the class and not of the function itself. Unless you are passing a new value through "accel" each time you call it, it will always remain the same. Really both firstCall and acceleration need to be part of the class and not part of the function. The distanceToCoordinates could remain either way.

    Code (csharp):
    1.  
    2. var distanceToCoordinates : float;
    3. var firstCall : boolean = false;
    4. var acceleration : float;
    5.  
    6. function MoveToTarget(coordinates : Vector3, accel : float, accuracy : float, reset : boolean)
    7. {
    8.     //If this is the first time that this function was called
    9.     //This is needed to make sure we don't reset each variable
    10.     //Every time it's called
    11.     if(!firstCall)
    12.     {
    13.         firstCall = true;
    14.         acceleration = accel;
    15.     }
    16.  
    17.     //Calculates the distance to the given coordinates
    18.     distanceToCoordinates = Vector3.Distance(transform.position, coordinates);
    19.     DebugLog("MissileSpeeder/MoveToTarget/distanceToCoordinates: " + distanceToCoordinates);
    20.     DebugLog("MissileSpeeder/MoveToTarget/acceleration: " + acceleration);
    21.     DebugLog("MissileSpeeder/MoveToTarget/firstCall: " + firstCall);
    22.  
    23.     if(!reset)
    24.     {
    25.         //Rotates the vehicle toward coordinates
    26.         targetRotation = Quaternion.LookRotation (coordinates - transform.position);
    27.         transform.localRotation = Quaternion.Lerp (transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
    28.  
    29.         //Moves the vehicle toward the coordinates
    30.         transform.Translate(Vector3.forward * (Time.deltaTime + acceleration));
    31.  
    32.         acceleration += 0.1;
    33.  
    34.     }else//Reset all variables back to default for next call
    35.     {
    36.         acceleration = 0;
    37.         firstCall = false;
    38.     }
    39. }
    40.  
    I think this is what you are looking for. Let me know if it helps.

    LordAelfric
    Starleaf Labs
     
    Last edited: Mar 6, 2012
  5. CgRobot

    CgRobot

    Joined:
    Jan 4, 2011
    Posts:
    175
    Yes! That was the problem. I moved firstCall and acceleration out of the function and now it works! Thanks very much! And you know what, that was kinda obvious. *sigh* I've been coding for too long.
     
    Last edited: Mar 6, 2012