So, I was just trying to add jump function to my game, but somehow it jumps twice everytime, I checked and the 'isGrounded' stays as true even when the player is in air.. Here's the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
// Floats and variables
[SerializeField]
private float speed = 5f;
private float xMov;
private float zMov;
public Vector3 Jump;
[SerializeField]
public float JumpForce = 2f;
public bool isGrounded;
Rigidbody rb;
// Start is called atr the start of the game
void Start()
{
rb = GetComponent<Rigidbody>();
Jump = new Vector3(0f, 2f, 0f);
}
void OnCollisionStay()
{
isGrounded = true;
}
// Update is called once per frame
void Update()
{
//Movement
xMov = Input.GetAxis("Horizontal");
zMov = Input.GetAxis("Vertical");
Vector3 movementValue = new Vector3(xMov, 0.0f, zMov);
transform.position += (movementValue * speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
isGrounded = false;
rb.AddForce(Jump * JumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Animations get screwed up when I add OnAnimatorMove function 0 Answers
Explosion on contact 0 Answers
How to move whilst airborne (using standard third person character script)? 0 Answers
My Capsule/Camera Flips out When Turning Around 1 Answer
How can I make an infinite floor that generates based on where the player goes? 1 Answer