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

Rigidbody FPS Controller, jumping

Discussion in 'Scripting' started by Somenic, Feb 3, 2021.

  1. Somenic

    Somenic

    Joined:
    Nov 8, 2020
    Posts:
    15
    So, I I've had the same problem since last week but only now I found what was the issue, my player only jumps If I set the ground layer the same as the player, but, now I jump infinetly. I don't know If it's something about Physics in my project or is a problem with the script


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMove : MonoBehaviour
    6. {
    7.     private Rigidbody rb;
    8.     public float moveSpeed = 10f;
    9.     public LayerMask ground;
    10.     public bool isGrounded;
    11.     public float jumpForce = 5f;
    12.  
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         // input
    21.         float x = Input.GetAxisRaw("Horizontal") *moveSpeed;
    22.         float y = Input.GetAxisRaw("Vertical") * moveSpeed;
    23.  
    24.         //Check ground
    25.         isGrounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.4f, ground);
    26.  
    27.         //jumping
    28.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded) {
    29.             rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
    30.             Debug.Log(isGrounded);
    31.         }
    32.  
    33.         // moving
    34.         Vector3 movePos = transform.right*x+ transform.forward*y;
    35.         Vector3 newMovePos = new Vector3(movePos.x, rb.velocity.y, movePos.z);
    36.  
    37.         rb.velocity = newMovePos;
    38.        
    39.     }
    40. }
    Unity.PNG
    And I have the ground set as "Ground" like It shows in the script of the image
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    You're not setting your isGrounded boolean to false after you've pressed space, that's likely why you're jumping infinitely because the boolean isn't resetting.
     
    Last edited: Feb 3, 2021
  3. Somenic

    Somenic

    Joined:
    Nov 8, 2020
    Posts:
    15
    I don't think that's the problem because It doesn't recognise the Ground layer as the ground and the bool never activates. It only works when I say that Ground is the layer "Player" and it uses the player as Ground, so It jumps infinetly
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    In that case the problem might be your setup, have you considered using a raycast instead? Might work more accurately, could be worth a try. Alternatively you could use a physics collider as a trigger and have it set the boolean when it comes into contact with the ground layer.