Player jumping issue
I am new to c# and Unity and i am experimenting making a game. Everything is going great but i am having an issue with jumping. Here is my code:
if (Input.GetKeyDown ("space"))
{
a.AddForce (0, jump, 0);
}
jump is a public float and a is the player's public Rigidbody and some times it uses the public float jump but other times the player jumps a lot higher. Thanks in advance.
Answer by dan_wipf · Mar 24, 2019 at 07:32 AM
you might have a look at some tutorials..
but this should get you going
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour {
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay()
{
isGrounded = true;
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
}
Your answer
Follow this Question
Related Questions
Unity Rigidbody fps controller jumps higher each time. 0 Answers
Error CS0120 : An object reference is required to access non-static member 3 Answers
Is it possible to make an Okami Brush mechanic in Unity Engine? 0 Answers
how i can jump? 0 Answers
How to double jump? i just can jump once when it touch the ground.. help me guys =D 1 Answer