Search Unity

Basic scripting problem

Discussion in 'Scripting' started by jeremyace, Oct 20, 2005.

  1. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    I am just learning unity scripting, and for some reason I can't get my script to work.

    Here it is:

    Code (csharp):
    1.  
    2.  
    3. var lastRunTime = 0;
    4. var startTime = 0;
    5. var upPower = 90;
    6. var forwardPower = 70;
    7. var jumpDuration = 2;
    8.  
    9.  
    10. function FixedUpdate() {
    11.     //check how long since we fired the jump script
    12.  
    13.     if (lastRunTime == 0){  // if this is the first time we have run this script
    14.         lastRunTime = Time.time;  //set the lastRunTime to this time
    15.         jump();
    16.     }
    17.     else if (Time.time - lastRunTime >= lastRunTime + 5)  {    // first get time passed since last run, see if 5 seconds have passed since last run.
    18.         lastRunTime = Time.time;
    19.         jump();
    20.     }
    21.  
    22. return;
    23. }
    24.  
    25.  
    26.  
    27. function jump() {
    28.  
    29.     startTime = Time.time;  //set the start time for the loop below
    30.    
    31.     while(Time.time - startTime <=  jumpDuration){    // keep looping until we get to jumpDuration (the lengh of time we loop for)
    32.      rigidbody.AddForce (Vector3.up * upPower);     // add vertical force
    33.      rigidbody.AddForce(Vector3.right * forwardPower);        //add right force
    34.     }
    35.    
    36. return;
    37. }
    38.  
    39.  
    What that code does (supposed to do) is make a frog jump every 5 seconds. It actually locks up Unity, and I can't find any endless loop, and it behaves erratically when I comment out the while loop as well.

    I also tried update(), start(), and StartCoroutine(), no dice. Using Start() with a yield in the while loop doesnt freeze up, but it doesn't do anything either.

    I am sure I am doing some basic thing wrong, so any help will be great :)

    Thanks,
    -Jeremy
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Ok, I fixed your suggestions and it doesn't lock up anymore, but it doesn't totally work either.

    What's happening now is the jump routine works fine the first run, but when it is called again after the 5 seconds it doesn't move. The jump routine is also not called consistantly. My startTime var goes 1, 7, BIG DELAY, 19, HUGE DELAY. It is supposed to jump once every second.

    Am I doing something wrong with the Time class?

    Here is the changed code:

    Code (csharp):
    1.  
    2. var lastRunTime = 0;
    3. var startTime = 0;
    4. var upPower = 90;
    5. var forwardPower = 70;
    6. var jumpDuration = 2;
    7.  
    8.  
    9. function FixedUpdate() {
    10.    //check how long since we fired the jump script
    11.  
    12.    if (lastRunTime == 0){  // if this is the first time we have run this script
    13.       lastRunTime = Time.time;  //set the lastRunTime to this time
    14.       StartCoroutine ("jump");
    15.    }
    16.    else if (Time.time - lastRunTime >= lastRunTime + 5)  {    // first get time passed since last run, see if 5 seconds have passed since last run.
    17.       lastRunTime = Time.time;
    18.       StartCoroutine ("jump");
    19.    }
    20.  
    21. return;    
    22. }
    23.  
    24.  
    25. function jump() {
    26.  
    27.    startTime = Time.time;  //set the start time for the loop below
    28.    
    29.    while(Time.time - startTime <=  jumpDuration){    // keep looping until we get to jumpDuration (the lengh of time we loop for)
    30.     rigidbody.AddForce (Vector3.up * upPower);      // add vertical force
    31.     rigidbody.AddForce(Vector3.right * forwardPower);         //add right force
    32.    yield WaitForFixedUpdate();
    33.    }
    34.    
    35. yield;
    36. }
    37.  
    Thanks for the help,
    -Jeremy
     
  4. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Everytime you jump, the time you are comparing to
    Code (csharp):
    1. lastRunTime + 5)
    is getting larger. So:

    Jumps already / lastRunTime (approx) / computed value
    1 / 0 / 5
    2 / 5/ 10
    3 / 10/ 15
    4 / 15 / 20

    Change it the line to
    Code (csharp):
    1. else if (Time.time - lastRunTime >= 5)
    and see if it works closer to how you expect.

    Also, you might want to change your time variables to floats (ie.default to 0.0, etc.) The script is flaky for me when the time vars are ints.
     
  5. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Thanks for the reply lfrog (<--leapfrog? lol kind of ironic. This is a frog hopping script ;))

    I guess I didn't really explain my code properly, sorry :)
    I need my jump() routine to ONLY trigger if if 5 seconds have passed since last jump.

    So:
    Code (csharp):
    1.  
    2. Time.time - lastRunTime >= lastRunTime + 5
    3.  
    ...takes the current time and subtracts the lastRunTime from it to get the time passed since last jump, and if that == 5 seconds since last jump then we can do another jump.

    this:
    Code (csharp):
    1.  
    2. Time.time - lastRunTime >= 5
    3.  
    ...only works as a "one shot" as the number keeps building as we go so it will only be under 5 for so long. I want my code to keep "hopping" until the world ends or the power goes out. :wink:

    I am sure there is probably an easier way to do all this, I think the problem is somehow my use of the Time.time class somehow?

    -Jeremy
     
  6. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Thanks for the reply lfrog (<--leapfrog? lol kind of ironic. This is a frog hopping script ;))

    I guess I didn't really explain my code properly, sorry :)
    I need my jump() routine to ONLY trigger if if 5 seconds have passed since last jump.

    So:
    Code (csharp):
    1.  
    2. Time.time - lastRunTime >= lastRunTime + 5
    3.  
    ...takes the current time and subtracts the lastRunTime from it to get the time passed since last jump, and if that == 5 seconds since last jump then we can do another jump.

    this:
    Code (csharp):
    1.  
    2. Time.time - lastRunTime >= 5
    3.  
    ...only works as a "one shot" as the number keeps building as we go so it will only be under 5 for so long. I want my code to keep "hopping" until the world ends or the power goes out. :wink:

    I am sure there is probably an easier way to do all this, I think the problem is somehow my use of the Time.time class somehow?

    -Jeremy
     
  7. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    UPDATE:

    I have to apologize lfrog, your change DID fix it. I was getting some very weird physics problems, force required (seemed like the "frog" was randomly getting heavier) and my scripts wouldn't take the changes I was making? I had to manually remove the script component from the box and re-apply it to get the changes to register. Weird. That problem wasn't consistent either. So I was fixing problems that weren't even there anymore :?

    Anyway, thank's for the help!

    -Jeremy
     
  8. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    No problem, I am glad I was able to help. One thing I have noticed when changing scripts, it takes a several seconds (maybe 8-10) before the changes are reflected. It is easiest to see this when you change Debug statements. My guess is it has to do with the compiling. Stopping and starting the simulations seems to speed up the change, but that could be subjective.
     
  9. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Yeah, I found that out the hard way ;)
    The weird thing is some of my changes would show instantly, while others (similar changes) would not. It's fun when you are trying to debug using old output, lol.

    -Jeremy
     
  10. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    If you look at the very bottom-right corner of Unity, there is a small wait-rotating thingy that shows when a compile is happening.
     
  11. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Thanks nicholas, I thought that was just the progress of error checking, not compiling, makes things easier. :oops: