• 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
Question by diddydale_95 · Feb 26, 2014 at 09:30 AM · variabletimejumpheight

Creating a variable jump using deltaTime in C#.

Hey everyone, I'm currently working on a game for my college project (some of you may know from my previous question) and I want to create a variable jump height that is controlled using deltaTime. So the longer the player holds the spacebar down, the higher the jump will be. My current code is below and I am basically stumped from here, any help will be greatly appreciated. Thank you.` private bool Jumping; private bool falling;

 // Use this for initialization
 void Start () {

     Jumping = false;//start off not jumping
     Falling = false;//start off not falling
 }
 
 // Update is called once per frame
 void Update () {
     print (transform.position.y);

     if (Input.GetKeyDown (KeyCode.Space)) { //if the player presses the Space Bar
         if (!Jumping) { //and if you're not already jumping
             rigidbody.AddForce(0, 500, 0);//apply an upward force to the runner
             Jumping = true;//now you're jumping
         }
         print ("Falling");

         if (!Jumping && transform.position.y>0.5) {//if the player has released the space bar after jumping
             Falling = true;//then the player is falling
         //if (Input.GetKeyDown (KeyCode.Space) && transform.position.y<1.6){//if jumping and player is higher than 3 
             //rigidbody.AddForce(0, 300, 0);//apply more upward force
         //}
     }
 }

 void OnCollisionEnter (){ //when the player collides with anything
     Jumping = false;//then you are not jumping
     Falling = false;//then you are no longer falling 

     }
 }`
Comment

People who like this

0 Show 1
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 diddydale_95 · Feb 26, 2014 at 10:08 AM 0
Share

I have applied a change to this coding, trying to keep it as basic as possible.` public Vector3 jumpVelocity; private bool touchingPlatform;

 // Use this for initialization
 void Start () {

     touchingPlatform = true;
 }
 
 // Update is called once per frame
 void Update () {

     if (Input.GetButtonDown ("Jump")) { //if the player presses the Space Bar
         if (touchingPlatform) { //and if you're touching the platform
             rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);//apply an upward force to the runner
         }
     }
 }

 void OnCollisionEnter (Collision col){ //when the player collides 
     if (col.gameObject.name == "cube") {//with a game object called cube
         touchingPlatform = true;//then you are touching the platform
     }
 }

}`

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Making a character jump with the Character Controller Component instead of Capsule Collider 0 Answers

My variable doesn't update real-time? 2 Answers

Change character controller height when Jumping 3 Answers

How to: make changes to a var over real time units instead of frame units? Mathf.Lerp 1 Answer

Serialized variables not sharing 2 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