Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Jaidan · Jan 31, 2015 at 10:36 AM · jumpjumpingheightsameequal

Fixed height jumping? 2D C#

Hello, I was wondering if anyone new how to keep all the jumps in a 2d game the same height. My character is never grounded and is always in the air and I want to be able to jump as many times as I want, but I need the height of the jumps to all be the same. I am also using the rigid body to move it as I need collisions. Thanks and I hope I make sense

Comment
Add comment · Show 4
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 Scribe · Jan 31, 2015 at 10:39 AM 0
Share

You could simply set the y velocity of your rigidbody to some constant, that would ensure the same height jump. Or set the y component of velocity to 0 and then use AddForce with some constant. If you want to do it mathematically it is a little more complicated but you only have to solve a couple of equations, google 'SUVAT equations'.

avatar image Jaidan · Jan 31, 2015 at 10:50 AM 0
Share

Okay, well here is my code, how would i do that would i do that? Sorry for the trouble im a bit of a noob (I just have the grounded part, but im not using it)

using UnityEngine; using System.Collections;

public class Jumping : $$anonymous$$onoBehaviour {

 // Player Jump
 public int jumpHeight;
 public bool isGrounded;

 // Update is called once per frame
 void OnCollisionStay (Collision collisionInfo)
     {
         isGrounded = true;
     }
 
 void OnCollisionExit (Collision collisionInfo)
     {
         isGrounded = false;
     }
 void CheckIfGrounded (){


 }
 void Update () {
     // Left Control
     if (Input.Get$$anonymous$$ey("up")){
         rigidbody.AddForce(new Vector3(0, jumpHeight,0));
     }
 }

}

avatar image Scribe · Jan 31, 2015 at 11:30 AM 1
Share

perhaps the following:

 public int jumpHeight;
 float jumpSpeed;
 Vector3 velocity;
 
 void Start(){
     /*
     s = jumpHeight
     u = u
     v = 0
     a = Physics.gravity.y
     t = ~
     
     Equation without t is:
     
     u^2 = v^2 - 2as
     
     hence as v = 0 (we want speed to be 0 so that its at the 'top' of the jump)
     
     u = sqrt(-2as)
     */
     
     jumpSpeed = $$anonymous$$athf.Sqrt(-2*Physics.gravity.y*jumpHeight) + 0.1f;
 }
 
 void Update(){
     if(Input.Get$$anonymous$$eyDown("up")){
         velocity = rigidbody.velocity;
         velocity.y = jumpSpeed;
         rigidbody.velocity = velocity;
     }
 }

The 0.1 I added in start is just some buffer value as unity physics takes into account a few more things than SUVAT equations do, so its probably not quite right due to drag or similar. Either way it should jump the same height each time, so you can mess around with the values to get the correct 'same' height.

avatar image Jaidan · Jan 31, 2015 at 11:38 AM 0
Share

O$$anonymous$$G O$$anonymous$$G O$$anonymous$$G, I literally dont know how to thank you, the code is amazing, thank you soooo much. You literally dont know how happy i am right now, ive been trying to do this for ages. Thank you so much, i will definetely give you credit when i finally release my game. Thank you

1 Reply

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

Answer by DESIMA · Jan 31, 2015 at 04:53 PM

If you just wanted to controll height you can do that with Damping and mass for the rigidbody, and when you add and rigidbody.AddForce(); you can decide on a certain number for velocity

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I limit jump height in unity? 3 Answers

Player Inconsistent Jumps 0 Answers

Adding a jump feature help? 2 Answers

Jumping in only the direction of the last key pressed 0 Answers

Hey Guys I have a problem with a single jump script. 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