Search Unity

This script crashes when I run it.

Discussion in 'Scripting' started by Rastafari, Jul 14, 2013.

  1. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    Code (csharp):
    1.     var speed : float = 5.0;
    2.     var rotationSpeed : float = 50.0;
    3.     function Update () {
    4.  
    5.         var translation : float = Input.GetAxis ("Vertical") * speed;
    6.         var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    7.        
    8.  
    9.         translation *= Time.deltaTime;
    10.         rotation *= Time.deltaTime;
    11.  
    12.         transform.Translate (0, 0, translation);
    13.         transform.Rotate (0, rotation, 0);
    14.        
    15.        
    16.         if(Input.GetKeyDown(KeyCode.LeftShift)){            // Problem is here obviously.
    17.         speed = speed + 10;
    18.         }
    19.     }



    Its something with the left shift.
    I think its just doesnt stop adding speed.
     
  2. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    I don't know why it crashes but the problem is not in the GetKeyDown block
     
  3. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    Well, when I run it I can walk around properly but when I press the sprint button my game freezes.
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    So no error appears, it just freezes?
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    speed = 10;

    not

    speed = speed + 10;

    also, once you start running, you will never be able to slow down again... This could be the problem, as well as a logic flaw
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Or speed += 10; that will add ten to speed.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    No. That's the same code he already has written a different way (ie wrong)


    I might consider renaming the rotation variable as well... Cant see that it would conflict with the rotation member variable of Transform, but stranger things have happened.
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Why is that wrong James? Why would that crash the game? Any reason or just being snippy again? Seriously, there is nothing wrong with the code I suggested Rasta and I am unsure what the issue is with your code.
     
  9. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Code seems fine. However you should set it to = 10 as mentioned and then on GetKeyUp set it back to 5
     
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Right,
    It depends on the effect.
    If sprint is 10 always then James and BFGames are correct.

    If you want to add 10 to the current speed each time you press, then what you were doing is correct. As for your error, I see none.

    Cheers.
    :)
     
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I dont think its actually the cause of the freeze. I was pointing out it might be wrong.

    I wasnt being snippy (but now I am going to be). The solutions and comments I see you put in scripting are often wrong, or mis-informed.


    As best I can tell, there are several logic flaws in the code, but I am making an assumption on how the character should run... ie, when holding sprint key, runs fast, when not, runs at normal speed. Ive never seen a game that continually increments the characters sprint speed.

    Anyway, here is how I would write the code.

    Code (csharp):
    1.  
    2.  
    3.  var runSpeed : float = 5.0;
    4.  var sprintSpeed : float = 10.0;
    5.  var rotationSpeed : float = 50.0;
    6.  
    7.  function Update () {
    8.         var speed : float = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : runSpeed;
    9.  
    10.         var translation : float = Input.GetAxis ("Vertical") * speed * Time.deltaTime;
    11.         var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed * Time.deltaTime;
    12.  
    13.         transform.Translate (0, 0, translation);
    14.         transform.Rotate (0, rotation, 0);
    15. }
    16.  
    17.  
     
  12. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
  13. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    Hey thanks for all the help.
    Sorry to be a pain in the butt but what does this line do?
    Code (csharp):
    1. var speed : float = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : runSpeed;
     
  14. Mister-E2

    Mister-E2

    Joined:
    Jun 6, 2013
    Posts:
    179
    That is identical to the following:

    Code (csharp):
    1.  
    2. var speed : float;
    3. if(Input.GetKey(KeyCode.LeftShit)
    4. {
    5.    speed  = sprintSpeed;
    6. }
    7. else
    8. {
    9. speed = runSpeed;
    10. }
    11.  
    12.