Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Jump Function

Discussion in 'Scripting' started by TechnoObi, Oct 11, 2014.

  1. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Hello, I have this code, rigidbody and character controller, but my character doesn't jump.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Jump : MonoBehaviour {
    6.  
    7.     public float speed = 6.0F;
    8.     public float jumpPower = 8.0F;
    9.     public float gravity = 20.0F;
    10.     Vector3 moveDirection = Vector3.zero;
    11.     public bool inputJump = false;
    12.     CharacterController characterController;
    13.     void Update()
    14.     {
    15.         characterController = GetComponent<CharacterController>();
    16.         if (Input.GetKeyDown(KeyCode.UpArrow))
    17.         {
    18.            
    19.             inputJump = true;
    20.         }
    21.         else
    22.         {
    23.             inputJump = false;
    24.         }
    25.         if (characterController.isGrounded)
    26.         {
    27.             if (inputJump)
    28.             {
    29.                 moveDirection.y = jumpPower;
    30.             }
    31.         }
    32.         moveDirection.y -= gravity * Time.deltaTime;
    33.  
    34.        
    35.     }
    36. }
    37.  
    38.  
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
  3. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    The script worked for my old game, but now it doesn't work. Here is the official code from Unity:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ExampleClass : MonoBehaviour {
    6. public float speed = 6.0F;
    7. public float jumpSpeed = 8.0F;
    8. public float gravity = 20.0F;
    9. private Vector3 moveDirection = Vector3.zero;
    10. void Update() {
    11. CharacterController controller = GetComponent<CharacterController>();
    12. if (controller.isGrounded) {
    13. moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    14. moveDirection = transform.TransformDirection(moveDirection);
    15. moveDirection *= speed;
    16. if (Input.GetButton("Jump"))
    17. moveDirection.y = jumpSpeed;
    18.  
    19. }
    20. moveDirection.y -= gravity * Time.deltaTime;
    21. controller.Move(moveDirection * Time.deltaTime);
    22. }
    23. }
    24.  
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Line 21:
    "controller.Move(moveDirection * Time.deltaTime);"

    That's what's missing in your current script.
    This should be obvious already, therefore i do recommend you to learn how to
    code, or use a plugin to do it for you. And if that still doesn't satisfy you, then you should start with a more simple game-engine. So you can work your way up > :l
     
    TechnoObi likes this.
  5. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Thanks. I haven't used for a few month and just didn't see the line. But thanks for your help :D
     
    Magiichan likes this.