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

speed problem

Discussion in 'Scripting' started by Falin, Mar 15, 2011.

  1. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    heey people,
    I openend an old project and played the level. I found that i moved slowly so I adjusted the speed but then he wouldn't jump right anymore. So I adjusted the Jumpspeed, didn't helped. then I played around with the gravity but I can't find the jumpheight it had by: Speed= 6.2, JumpSpeed = 7, Gravity = 25.
    Code (csharp):
    1. /// This script moves the character controller forward
    2. /// and sideways based on the arrow keys.
    3. /// It also jumps when pressing space.
    4. /// Make sure to attach a character controller to the same game object.
    5. /// It is recommended that you make only one call to Move or SimpleMove per frame.    
    6. var speed = 6.0;
    7. var jumpSpeed = 8.0;
    8. var gravity = 20.0;
    9.  
    10. private var moveDirection = Vector3.zero;
    11.  
    12. var hi = false;
    13. //function OnControllerColliderHit(hit : ControllerColliderHit)
    14. function OnTriggerEnter(hit : Collider)
    15. {
    16. if(hit.gameObject.tag == "bullit")
    17. {
    18. stats.health -= 15;
    19.  
    20. }
    21. if(hit.gameObject.tag == "Spikes")
    22. {
    23. stats.health -= 30;
    24. }
    25. if(hit.gameObject.tag == "Potion")
    26. {
    27. stats.health = stats.max_health;
    28. }
    29.  
    30. }
    31. function FixedUpdate() {
    32.     var controller : CharacterController = GetComponent(CharacterController);
    33.     if (controller.isGrounded) {
    34.         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    35.                                 speed-5);
    36.         moveDirection = transform.TransformDirection(moveDirection);
    37.         moveDirection *= speed;
    38.        
    39.         if (Input.GetButton ("Jump")) {
    40.             moveDirection.y = jumpSpeed;                
    41.          }
    42.     }
    43.  
    44.    
    45.     moveDirection.y -= gravity * Time.deltaTime;
    46.    
    47.    
    48.     controller.Move(moveDirection * Time.deltaTime);
    49. }
    I don't know if anyone can helped me. but if you have suggestions, they are welcome. :)
     
  2. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    Hi,
    The problem is that when i press a key it goes at once very fast(Look at the vid and it happens just once at the begin):



    Code (csharp):
    1. // This script moves the character controller forward
    2. /// and sideways based on the arrow keys.
    3. /// It also jumps when pressing space.
    4. /// Make sure to attach a character controller to the same game object.
    5. /// It is recommended that you make only one call to Move or SimpleMove per frame.    
    6. var speed = 13;
    7. var jumpSpeed = 8.0;
    8. var gravity = 20.0;
    9.  
    10. private var moveDirection = Vector3.zero;
    11.  
    12. var hi = false;
    13. //function OnControllerColliderHit(hit : ControllerColliderHit)
    14. function OnTriggerEnter(hit : Collider)
    15. {
    16. if(hit.gameObject.tag == "bullit")
    17. {
    18. stats.health -= 15;
    19.  
    20. }
    21. if(hit.gameObject.tag == "Spikes")
    22. {
    23. stats.health -= 30;
    24. }
    25. if(hit.gameObject.tag == "Potion")
    26. {
    27. stats.health = stats.max_health;
    28. }
    29.  
    30. }
    31. function FixedUpdate() {
    32.     var controller : CharacterController = GetComponent(CharacterController);
    33.     if (controller.isGrounded) {
    34.         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    35.                                 speed-5);
    36.         moveDirection = transform.TransformDirection(moveDirection);
    37.         moveDirection *= speed;
    38.        
    39.        /* if (Input.GetButton ("Jump")) {
    40.             moveDirection.y = jumpSpeed;                
    41.          }*/
    42.     }
    43.  
    44.    
    45.     moveDirection.y -= gravity * Time.deltaTime;
    46.    
    47.    
    48.     controller.Move(moveDirection * Time.deltaTime);
    49. }
    What could be the problem?
     
  3. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    Time.deltaTime probably spikes on your first update. Print it and see if that is the case.
     
  4. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    ok i printed it out and got on the time that it goes fast around 0.008 and normal around 0.004