Search Unity

Making the character controller jump

Discussion in 'Scripting' started by thesimi, Aug 14, 2011.

  1. thesimi

    thesimi

    Joined:
    Jun 16, 2009
    Posts:
    94
    hi fellow unity3d users:D

    I'm working on a 2d platformer game, but I ran into a little problem when I was working on the controller, I can't make it jump. I have never programmed character controllers before and I am still trying to learn how they work, but I can't find out what the best solution is.

    here's my code untill now:
    Code (csharp):
    1. var Speed = 3.0;
    2. var JumpSpeed = 3.0;
    3.  
    4. function Update () {
    5.    
    6.     var controller : CharacterController = GetComponent(CharacterController);
    7.    
    8.     //Movement in the x-axis
    9.     var right = transform.TransformDirection(1, 0, 0);
    10.     var CurSpeed = Speed * Input.GetAxis("Horizontal");
    11.     controller.SimpleMove(right * CurSpeed);
    12.    
    13.     //Jumping controlls
    14.     //Check if the character controller is on the ground
    15.     if (controller.isGrounded) {
    16.         if (Input.GetButton ("Jump")) {
    17.             //problem------->       what should I add here to make the character controller jump?
    18.         }
    19.     }
    20. }
    21.  
    22. @script RequireComponent(CharacterController);
     
  2. Zaffer

    Zaffer

    Joined:
    Oct 21, 2010
    Posts:
    266
    Hi Simi,

    I got this from the Unity script reference at http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html Here is the code example they provide. Hope you can use it.
    Zaffer
    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.  
    7. var speed : float = 6.0;
    8. var jumpSpeed : float = 8.0;
    9. var gravity : float = 20.0;
    10.  
    11. private var moveDirection : Vector3 = Vector3.zero;
    12.  
    13. function Update() {
    14. var controller : CharacterController = GetComponent(CharacterController);
    15. if (controller.isGrounded) {
    16. // We are grounded, so recalculate
    17. // move direction directly from axes
    18. moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    19. Input.GetAxis("Vertical"));
    20. moveDirection = transform.TransformDirection(moveDirection);
    21. moveDirection *= speed;
    22.  
    23. if (Input.GetButton ("Jump")) {
    24. moveDirection.y = jumpSpeed;
    25. }
    26. }
    27.  
    28. // Apply gravity
    29. moveDirection.y -= gravity * Time.deltaTime;
    30.  
    31. // Move the controller
    32. controller.Move(moveDirection * Time.deltaTime);
    33. }
     
  3. thesimi

    thesimi

    Joined:
    Jun 16, 2009
    Posts:
    94
    thanks a lot :D
    no idea how I didn't think of that :p