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

I was just following along to Bracky's fps movement tutorial

Discussion in 'Scripting' started by SimpleAnim, Apr 29, 2021.

  1. SimpleAnim

    SimpleAnim

    Joined:
    Apr 29, 2021
    Posts:
    2
    And everything seems to work, except for jumping. For whatever reason, I can't jump. I made sure the key input for jump was "space." I also tried other keys, and nothing worked. I tried changing the code to

    if(Input.GetKey("space") && isGrounded)

    I also tried putting a semicolon after " if(Input.GetButtonDown("Jump") && isGrounded)" but all that did was make me fly up forever without any input. Unity is confusing bruh. Just looking for what's wrong with this.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class PlayaMovement : MonoBehaviour
    {
    public CharacterController controller;
    public float speed = 12f;
    public float gravity = 9.81f;
    public float jumpHeight = 3f;
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    Vector3 velocity;
    bool isGrounded;
    // Update is called once per frame
    void Update()
    {
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    if(isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);
    if(Input.GetButtonDown("Jump") && isGrounded)
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }
    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
    }
    }
     
  2. stain2319

    stain2319

    Joined:
    Mar 2, 2020
    Posts:
    419
    The most likely problem, speaking from experience, is that "isGrounded" isn't working right.

    Either make "isGrounded" public or put [SerializeField] before it so you can see it in the inspector and I bet you'll find that it isn't showing grounded when you expect it to.

    Likely causes of this are: you need to reduce the skin width on the character controller or you need to adjust the Center and Height values of your character controller (which affects the capsule collider.). I like to use a value of 0.9Y for the center and 1.8 for the height but it may also depend on your character model.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  4. SimpleAnim

    SimpleAnim

    Joined:
    Apr 29, 2021
    Posts:
    2
    I supose the rest is useful for the future but there are no errors, it simply does not work.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    There's a really good way to track this stuff down, since it really happens a lot. In fact, I do it every single day. I write some code, I fix the syntax errors, I put it on the GameObject, I run my program and ... nothing happens. WTF?!

    Every. Single. Day.

    Remember there are like 57 billion ways this can fail, so here's what you do:

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    You could also just display various important quantities in UI Text elements to watch them change as you playtest.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  6. rth2

    rth2

    Joined:
    Apr 20, 2020
    Posts:
    1
    Hello!

    Gave it a quick glance but this part confuses me.

    You have :
    Code (CSharp):
    1. if(Input.GetButtonDown("Jump) && isGrounded)
    2. {
    3.  velocity.y = Mathf.sqrt(jumpHeight * -2f * gravity);
    4. }
    jumpHeight = 3.0f; gravity = 9.81f; -2f = well -2.0f;

    You appear to be setting to the square root of a negative number. This results in NaN (Not a Number). I assume that after you set to NaN that the behaviour is undefined.
     
  7. Omniglitch

    Omniglitch

    Joined:
    May 29, 2017
    Posts:
    37
    In your code the gravity variable is a positive number, and in your Update function it's being added to velocity.y. Since positive along the Y axis is up, that code is going to make you fall upwards. Setting gravity to -9.81f is how it's usually handled (setting gravity to a negative number, and adding that to the Y velocity).