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 MetalMario64 · Sep 22, 2014 at 09:03 PM · jumping

Jumping By Set Amount Instead of By Speed

I want to make my character jump by a set amount instead of by a velocity. The way it is now, the double jump when activated at the beginning of the first jump sends the character flying really high, but if used at the bottom of the first jump, does almost nothing at all. I want to make them jump by set amounts instead so that they jump the same way no matter when or where the jump takes place.

Here is all the code related to jumping that I have. What could I do to fix this? Thanks.

 //Ground Check Variables
 bool onGround = false;
 public Transform checkGround;
 float groundRadius = 0.2f;
 public LayerMask Ground;
 public float jumpSpeed = 225; //Jump Speed
 
 bool doubleJump = false;
 bool tripleJump = false;

 void Update () 
 {
 //Jump Control
   
  if ((onGround || !doubleJump || !tripleJump)  && Input.GetKeyDown (KeyCode.Space)) 
  {
     anim.SetBool ("Ground", false);
     rigidbody2D.AddForce (new Vector2(0, jumpSpeed));
                 
     if(!doubleJump && !onGround && !tripleJump)
     {
         jumpSpeed = 200;
         doubleJump = true;
     }
                 
     if(doubleJump && !onGround && !tripleJump)
     {
         jumpSpeed = 100;
         tripleJump = true;
     }
                 
     jumpSpeed = 225; //Resets the first jumps' jumpSpeed.
     }
 }


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

Answer by mouurusai · Sep 23, 2014 at 02:58 AM

Hi it's very interesting question. using UnityEngine;

 [RequireComponent(typeof(Rigidbody))]
 public class JumpTest : MonoBehaviour
 {
     public float jumpHeight = 3;
     public int jumpCountLimit = 3;
 
     private int _jumpCount;
 
     public float prevY;
     private float _totalY;
     private float _startY;
     
     void Update ()
     {
 
         Vector3 rbVel = rigidbody.velocity;
         rbVel.z = Input.GetAxis("Horizontal")*5;
         rigidbody.velocity = Vector3.MoveTowards(rigidbody.velocity, rbVel, (_jumpCount == 0 ? 20 : 8)*Time.deltaTime);
 
         float deltaY = prevY - transform.position.y;
         if (deltaY > 0)
             _totalY += deltaY;
         prevY = transform.position.y;
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             if(_jumpCount<jumpCountLimit)
                 Jump();
         }
     }
 
     void Jump()
     {
         if(_jumpCount==0)
         {
             _startY = transform.position.y;
             _totalY = 0;
         }
 
         ++_jumpCount;
         float g = Physics.gravity.y;
         float h = jumpHeight * _jumpCount - _totalY - (transform.position.y-_startY);
 
         float v = Mathf.Sqrt(-2*g*h);
 
         Vector3 newVel = rigidbody.velocity;
         newVel.y = v;
         rigidbody.velocity = newVel;
     }
 
     void OnCollisionEnter()
     {
         _jumpCount = 0;
     }
 } 
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

Player is overjumping (jumping at least twice when it shouldn't) 2 Answers

2D:I have a movementController script on my character but it doesn't move forward 0 Answers

How to make a 2Dgun shoot at the position of mouse? 3 Answers

jump and check ground problem! 2 Answers

How do i code jumping for a 2d game in JS? 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