Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by DameyC · Nov 01, 2015 at 10:17 PM · c#jumping

How do I make my player jump at a fixed height, and only once until he touches the ground again?

I have been trying all day and all yesterday trying to make my character jump at a fixed height for this 2D platformer I am making, I used this code (it's in C#)

if (Input.GetKeyDown(KeyCode.Space)){ transform.position += new Vector3(0, jumpHeight * Time.deltaTime, 0);

and he will jump a little bit at random heights even though I'm using a public float to change his jump height. I have been browsing the web trying and using the code in different tutorial videos and other people's posts but it doesn't seem to work. Also I've been trying to figure out how to make him jump once.

I hate to ask for help, but I'm still very new to all of this and I'm just not getting anywhere. Any help or suggestions would be greatly appreciated.

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

1 Reply

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

Answer by van der Merwe · Nov 02, 2015 at 09:10 PM

Well first off you would want to add a rigid body to your character ( this helps with physics such as gravity and so on)

Then the code would look something like this

 public float moveSpeed;
 public float jumpHeight;
 public float moveVelocity;
 
 bool grounded;
 
 //This is for 2D not 3D, I am just taking a wild guess this is a 2D game
 private RigidBody2D rigid;
 
 void Start()
 {
 
 //Get the rigidbody of character 
 rigid = GetComponent<RigidBody2D>();
 
 //Set grounded to true or false (Depending if your character starts in the 
 //air
 grounded = true;
 }
 
 void Update()
 {
 
 
 moveVelocity = 0;
 
 // If the character moves right 
 if (Input.GetKeyDown(KeyCode.D))
 {
 
 moveVelocity = moveSpeed;
 
 }
 // If the character moves left;
 if (Input.GetKeyDown(KeyCode.A))
 {
 
 moveVelocity = -moveSpeed;
 
 }
 
 //If you press spacebar and your are on the ground then you can jump
 if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
 {
 //I am not to sure about the rigid.velocity code play around with it
 //I think in 2D you can set it to a Vector2 instead of Vector3 otherwise just 
 //change it
 rigid.velocity = new Vector2 (moveVelocity, jumpHeight);
 
 //After your character jumps grounded has to change to false since the 
 //character is not on the ground anymore
 grounded = false;
 
 }
 
 // If your character is not falling nor jumping then change grounded true
 //Since he has landed.
 if (rigid.velocity.y == 0)
 {
 
 grounded = true;
 
 }
 
 }

Since I am not near unity I am not sure of the spelling of all the code I written but it should be more or less correct. I added comments in the code to explain each part.

Also place the your movement script onto your character (Otherwise RigidBody2D won't know what your trying to code)

Hope this help

Comment
Add comment · Show 3 · 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 DameyC · Nov 03, 2015 at 07:14 AM 0
Share

Thanks for the help :)

avatar image van der Merwe DameyC · Nov 03, 2015 at 12:05 PM 0
Share

Pleasure, strongs with future development

avatar image mnmwert · Apr 08, 2016 at 07:21 PM 0
Share

thank you so much ive been looking for an answer to my problem for a long time!!

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

33 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 make it so I can't use the jetpack/jump while slider value is 0? 0 Answers

Pressing Vs Jumping 1 Answer

Still constant jumping even with raycasting. 0 Answers

Anyone have any ideas on how I could improve my character jump to make it more realistic? 1 Answer

How can I make my Player jump when I press a UI button? 0 Answers


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