Search Unity

What am I missing? Cant jump

Discussion in 'Scripting' started by Agentneil, Apr 18, 2018.

  1. Agentneil

    Agentneil

    Joined:
    Apr 18, 2018
    Posts:
    9
    What am I missing here? Unity isn't giving me any scripting errors. But I cant get off the ground

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ActorMovement : MonoBehaviour {
    6.  
    7.     public float speed = 10;
    8.     public float jumpForce = 10f;
    9.  
    10.     public bool isGrounded;
    11.  
    12.     public Vector3 jump;
    13.     CharacterController controller;
    14.     Rigidbody rb;
    15.  
    16.     void Start () {
    17.         controller = GetComponent<CharacterController>();
    18.         rb = GetComponent<Rigidbody>();
    19.         jump = new Vector3(0.0f, 3.0f, 0.0f);
    20.     }
    21.  
    22.     void OnCollisionStay()
    23.     {
    24.         isGrounded = true;
    25.     }
    26.  
    27.     void Update () {
    28.         float hAxis = Input.GetAxis("Horizontal");
    29.         float vAxis = Input.GetAxis("Vertical");
    30.  
    31.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    32.         {
    33.  
    34.             rb.AddForce(jump * jumpForce, ForceMode.Impulse);
    35.             isGrounded = false;
    36.         }
    37.  
    38.         controller.SimpleMove(transform.forward * vAxis * speed * Time.deltaTime);
    39.         transform.Rotate(transform.up, hAxis + Time.deltaTime);
    40.  
    41.     }
    42.  
    43. }
     
  2. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    Lets do some debugging!

    Why dont you add a debug log to see what is going on. For example:

    Code (csharp):
    1.  
    2. Debug.Log("Space: " + Input.GetKeyDown(KeyCode.Space) + ", isGrounded: " + isGrounded);
    3.  
    Compare that to what you think the values should be.
     
  3. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    isGrounded is a public variable. What is it's value when you're playing?
     
  4. Agentneil

    Agentneil

    Joined:
    Apr 18, 2018
    Posts:
    9
    You guys are quick,

    With the public var enabled it goes from:
    Space: False, isGrounded: True
    to:
    Space: False, isGrounded: False

    With it Disabled:
    Space: False, isGrounded: False
    to:
    Space: False, isGrounded: False
     
  5. Agentneil

    Agentneil

    Joined:
    Apr 18, 2018
    Posts:
    9
    No, hes got a texture
     
  6. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    Weird that "space" is "false" in all cases.

    Try using
    Code (CSharp):
    1. Input.GetKey(KeyCode.Space);
    instead of GetKeyDown.
     
  7. Agentneil

    Agentneil

    Joined:
    Apr 18, 2018
    Posts:
    9
    False still
     
  8. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
  9. Agentneil

    Agentneil

    Joined:
    Apr 18, 2018
    Posts:
    9
    Solved in the end by my teacher directing me to unity documentation examples
    Code (CSharp):
    1. [RequireComponent(typeof(CharacterController))]
    2. public class ActorMovement : MonoBehaviour
    3. {
    4.  
    5.     public float speed = 6.0F;
    6.     public float jumpSpeed = 8.0F;
    7.     public float gravity = 20.0F;
    8.  
    9.     private Vector3 moveDirection = Vector3.zero;
    10.     private CharacterController controller;
    11.  
    12.     private void Start()
    13.     {
    14.         controller = GetComponent<CharacterController>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.  
    20.         if (controller.isGrounded)
    21.         {
    22.           //  Debug.Log("Jump: " + Input.GetButton("Jump") + " ,isGrounded: " + controller.isGrounded);
    23.  
    24.             float rotation = Input.GetAxis("Horizontal");
    25.             transform.Rotate(transform.up, rotation);
    26.  
    27.             moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
    28.             moveDirection = transform.TransformDirection(moveDirection);
    29.             moveDirection *= speed;
    30.  
    31.             if (Input.GetButton("Jump"))
    32.                 moveDirection.y = jumpSpeed;
    33.         }
    34.  
    35.         moveDirection.y -= gravity * Time.deltaTime;
    36.         controller.Move(moveDirection * Time.deltaTime);
    37.     }
    38. }
     
  10. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    Thanks so much for posting your answer!