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

Can't get gravity and jump to work

Discussion in 'Game Design' started by Xactlly, Dec 20, 2021.

  1. Xactlly

    Xactlly

    Joined:
    Dec 19, 2021
    Posts:
    1
    I was working on a first person game as a rookie. I got the movement and mouse movement down but the velocity doesnt reset everytime I fall and hit the ground. My jump key will also not work. I am not sure if it because of the velocity and gravity problem or my code. (using C#)


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Movement : 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 * -2 * gravity);
    }

    velocity.y += gravity * Time.deltaTime;

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

     
  2. Steve_Stevens

    Steve_Stevens

    Joined:
    May 3, 2016
    Posts:
    35
    NEVER use public variables just to access them in the Inspector use
    [SerializeField] instead.

    You should have minimal code in your updates.

    Break all the steps down into methods and call them from update.
    You shouldn't create variables over and over again inside an update. In the future, it will lead to large garbage collection overhead.

    Re write this with a GetInput method, a bool IsGrounded() method, a Gravtiy method, and a controllermove method.

    Once you do that, you can concentrate on what is not working in it's own method without affecting anything else.

    Let me know what you find.

    BTW, I am big on learning it the RIGHT way rather than having to change your ways when you start working on something important.
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    Some of what you've said there is personal preference, but this particular bit is just incorrect. Those variables are value types, so they won't be allocated on the heap (and generate garbage).

    I do agree with [SerializeField]. :)

    - - -

    @Xactly, when jumping, does your player get far enough off the ground for isGrounded to be set to false?

    P.S., this should be in the "Scripting" area rather than "Game Design".