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 evand · Sep 24, 2013 at 08:26 PM · jumpingplatformerheight

How do I create variable Jump Height for a platformer?

I am trying to put together a platformer style game but am unable to get variable jump height to work. I can jump, I can double jump, but all my jumps are the exact same height. There is no "short hop" version by pressing the jump button and letting go quickly.

I am using C#, the code I am using is below.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(PlayerPhysics))]
 public class PlayerController : MonoBehaviour {
     
     //Player Handling
     public float gravity = 20;
     public float walkSpeed = 10.0f;
     public float runSpeed = 20.0f;
     public float acceleration = 30.0f;
     public float jumpHeight = 12.0f;
     public float jumpAirTime = 0f;
     public float jumpMaxAirTime = 0.2f;
     
     public float timeOffGround = 0f;
     private float currentSpeed;
     private float targetSpeed;
     private Vector2 amountToMove;
     
     //sloppy double jump stuff
     private bool djump = true; //This must be set to true or false to enable double jump; set to true on item pickup or the like for ingame toggle.
     public bool djumpused = true; //checks whether or not the jump has been used; set to true to any spawn off the ground
                                   //will not result in the ability to double jump
         
     private PlayerPhysics playerPhysics;
     
     void Start () 
     {
     playerPhysics = GetComponent<PlayerPhysics>();
     }
     
     void Update () 
     {        
         // Reset acceleration upon collision
         if (playerPhysics.movementStopped)
         {
             targetSpeed = 0;
             currentSpeed = 0;
         }
         
         
         if (playerPhysics.grounded)
         {
             amountToMove.y = 0;
             timeOffGround = 0;
             djumpused = false;
             
             
             //Jump
             if (Input.GetKeyDown(KeyCode.Space)) // If jump is pressed
             {
                 jumpAirTime = jumpMaxAirTime; //Start jump
                 jumpAirTime -= Time.deltaTime; //Timer decreases
                 
             }
         
         
         if (amountToMove.y == 0 && Input.GetKeyDown (KeyCode.Space) && jumpAirTime > 0) //If you are jumping
         {
             amountToMove.y = jumpHeight; //jumping force!
             jumpAirTime -= Time.deltaTime; //Timer decreases
         }
         
              else
                 {
                     amountToMove.y = 0;
                     jumpAirTime = 0;
                     timeOffGround =0;
                 }
         
                 
         }
         
         if (Input.GetKeyDown (KeyCode.Space) && djumpused == false && !playerPhysics.grounded)
             {
                 amountToMove.y = jumpHeight;
                 djumpused = true;
                 
             }
         
         else //gravity takes over at this point
         {
             timeOffGround += Time.deltaTime;
         }
         
         
         
         
         
         //Input
         float speed = (Input.GetButton ("Run"))?runSpeed:walkSpeed;
     targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
     currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
     
     
         
         amountToMove.x = currentSpeed;
         amountToMove.y -= gravity * Time.deltaTime;
         playerPhysics.Move(amountToMove*Time.deltaTime);
         
     }
             
     // Increase n towards target by speed
     private float IncrementTowards(float n, float target, float a)
     {
         if (n == target)
         {
             return n;
         }
         else
         {
             float dir = Mathf.Sign (target - n); //must n be increased or decreainsed to get closer to target
             n += a * Time.deltaTime * dir;
             return (dir == Mathf.Sign (target-n))? n: target; //if n has increased over target then return target, otherwise return n. The ? n: is a shorthand if statement.
             
     
         }
     }
 
 }
 
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 Hatdatsat · Sep 25, 2013 at 12:24 AM

Maybe make a timer, so if the spacebar is pressed really short like a few milliseconds then it will go x height. and if it's being pressed down long enough go full height. You have a static jump height in this script so it can't change unless you put in the script that it will be more or less over time. Now the script sees press button okay i will do this height, the time of the button is not factor so you need to make it 1. (sorry if my English is bad)

Comment
Add comment · Show 2 · 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 evand · Sep 25, 2013 at 12:50 AM 0
Share

Any advice on the best way to set it to be more or less over time?

I'd like to have a variable jump height rather than two specific jumps (similar to megaman or mario, where they jump until you let go of the button). If I was making a fighter your idea would be great though! :)

avatar image rednax20 · Sep 25, 2013 at 01:16 AM 0
Share

I'm afraid i don't have the stomach to down all your code, but you could use something like this...

 function Update ()  // i thinks its void update in c# right?
  {
     If (Input.Get$$anonymous$$ey(jumpbutton))
     {
              //increase objects height by an incrament
     }else{
              //lower the player
     }
 }

It's in java script ( i'm pretty sure it will still work though) and it might not work with how your are prefor$$anonymous$$g the jumping, but this may be a different way of looking at it, that might work better. sorry if this doesn't help. Good luck

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

17 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

Related Questions

auto jumping to a platform 0 Answers

2D Sidescroller enemy AI jump help! Picture Included! 3 Answers

Platformer cat not jumping and is flinging across the map when hitting a corner 2 Answers

2D jumping raycast question 0 Answers

How do i achieve a rounded jump arc? 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