How to make a simple jump script in 3D C#
I need to know how to make a simple jump script so i can make 3D game where you can jump.
Answer by aditya · Jun 24, 2016 at 05:19 AM
Warning : Untested code ... A direct copy N paste may make you to to ask a new question on this forum
Add gameobject, add Rigidbody to it, add script to it .... and in that script add this
public int forceConst = 50;
private bool canJump;
private RigidBody selfRigidbody;
void Start(){
selfRigidbody = GetComponent<RigidBody>();
}
void FixedUpdate(){
if(canJump){
canJump = false;
selfRigidbody.addForce(0, forceConst, 0, ForceMode.Impulse);
}
}
void Update(){
if(Input.GetKeyUp(Keycode.SPACE)){
canJump = true;
}
}
Take a look at AddForce
It says that there is an error: unexpected symbol 'int', expecting 'class', 'delegate', 'enum', 'interface', 'partial', or 'scruct'
This is because you may have paste the code as is without defining any class or may have removed the class definition while pasting
Holy typos Batman!
RigidBody should be Rigidbody
addForce() should be AddForce()
$$anonymous$$eycode should be $$anonymous$$eyCode
forceConst is not a const,.. not a typo, but still misleading.
canJump doesn't take in account if the object is grounded or not, you can jump again while jumping.
Reason for typos is that i have written this code right in unity answers without any editor in front of me ... as opposed to you said about canJump, i m not the programmer of his game and i m just suggesting him a way to do a specific task ... this portal is just for help and not a platform of freelancers ...
Never said it was a platform of freelancers. I'm just pointing out mistakes. A warning would've sufficed, e.g. Code is untested or pseudo. As you can see, the poor OP copy-pasted and had no idea what went wrong when their console exploded with errors... ... ...
Thanks! after fixing the typos, it worked!
if it worked than a click on Accept Answer
will help me to earn those super cool karma points @EpicCreeper127
I need help it says KeyCode does not exist in current context but i have tired all caps fixeis please help i know this is 4 years later but at least help
i think it has a problem that there are a glitch it has infinite jump it's like flying please answer
Answer by hnm938 · Jul 08, 2018 at 03:41 PM
public float jumpHeight = 7f;
public bool isGrounded;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (isGrounded)
{
if (Input.GetButtonDown("Jump"))
{
rb.AddForce(Vector3.up * jumpHeight)
}
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.tag == "Ground")
{
isGrounded = false;
}
}
Make sure to add a tag with the name "Ground" onto the ground object and your done.
Your answer
Follow this Question
Related Questions
How to use 3D colliders and rigidbody on 2D character controller 0 Answers
Need help with adding ground detection to my jump. (c#) (3D) 1 Answer
Jumping Function in my Player Controller Script isn't working 2 Answers
Button problem : changing scenes fail ? 0 Answers
Unity3d OnCollisionEnter not firing 1 Answer