Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by BananaBreadDev · Jun 24, 2016 at 05:07 AM · c#3djump

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BananaBreadDev · Jun 24, 2016 at 03:48 PM 0
Share

It says that there is an error: unexpected symbol 'int', expecting 'class', 'delegate', 'enum', 'interface', 'partial', or 'scruct'

avatar image aditya BananaBreadDev · Jun 27, 2016 at 05:00 AM 0
Share

This is because you may have paste the code as is without defining any class or may have removed the class definition while pasting

avatar image Jessespike · Jun 24, 2016 at 06:50 PM 1
Share

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.

avatar image aditya Jessespike · Jun 27, 2016 at 05:05 AM 0
Share

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 ...

avatar image Jessespike aditya · Jun 27, 2016 at 05:14 PM 0
Share

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... ... ...

Show more comments
avatar image BananaBreadDev Jessespike · Jun 27, 2016 at 08:02 PM 0
Share

Thanks! after fixing the typos, it worked!

avatar image aditya BananaBreadDev · Jun 28, 2016 at 05:46 AM 1
Share

if it worked than a click on Accept Answer will help me to earn those super cool karma points @EpicCreeper127

avatar image Moneyfalls123 Jessespike · Jun 13, 2020 at 04:32 AM 0
Share

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

avatar image everid · Mar 13, 2018 at 03:01 PM 1
Share

i think it has a problem that there are a glitch it has infinite jump it's like flying please answer

avatar image
6

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges