- Home /
My jumping script has errors
I have a player movement script in my game and a key aspect of the game is jumping however you cant because my script does not seem to work, I have a screenshot showing what may be a problem and also the script
 sing System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public CharacterController controller;
 
     public float speed = 12f;
     public float gravity = -9.81f;
     public float jumpForce = 100f;
 
     public Transform groundCheck;
     public float groundDistance;
     public LayerMask groundMask;
     public Rigidbody rb;
 
     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);
 
         velocity.y += gravity * Time.deltaTime;
 
         controller.Move(velocity * Time.deltaTime);
  
         if (Input.GetKeyDown("space"))
 
         {
             rb.AddForce(0, jumpForce, 0, ForceMode.Impulse);
         }
     }
 }

 
                 
                capsule.png 
                (297.7 kB) 
               
 
              
               Comment
              
 
               
              Answer by logicandchaos · Jan 27, 2020 at 12:15 AM
I think this might be the issue but I'm not sure
 if (isGrounded && velocity.y < 0)
          {
              velocity.y = -2f;
          }
also there is already gravity implemented in unity, unless you set that to 0 then you have double gravity, but ya I implemented gravity like that in my current project, but you should do it like this:
 if(!grounded)
     velocity.y += gravity * Time.deltaTime;
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                