Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ViperX · Dec 20, 2013 at 11:57 PM · jumpbeginnernoobyaxis

How do I define the speed of my player in the y axis?

I'm a real beginner but having great fun so far (=

What I want to do is be able to play around with- "if" statements to tell unity when my player can jump or not.

How do I define his speed in the y axis?

For example i'd like to do this:

if (speedY == 0 && Input.GetButton("Jump")) { jump; }

So basically only if he is standing still on the ground he could jump.

Thanks a lot in advance!

Comment
Add comment · 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 OP_toss · Jan 09, 2014 at 01:01 AM 0
Share

is this question closed? did I help? if so mark as correct please.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by CrazyKane · Jan 09, 2014 at 02:22 AM

 var notAirbourne = true;
 var jump: KeyCode;
     var jumpAmount: float = 12;
         var jumpRate: float = 1.2;
         private var nextJump = 0.0;
 
 if(Input.GetKey(moveRight) && Input.GetKey(jump) && Time.time > nextJump && notAirbourne == true){
                     notAirbourne = false;
                 nextJump = Time.time + jumpRate;
             rigidbody2D.velocity.x = moveSpeed;
             rigidbody2D.velocity.y = jumpAmount;
         }
         if(Input.GetKey(moveLeft) && Input.GetKey(jump) && Time.time > nextJump && notAirbourne == true){
                     notAirbourne = false;
                 nextJump = Time.time + jumpRate;
             rigidbody2D.velocity.x = moveSpeed * -1;
             rigidbody2D.velocity.y = jumpAmount;
         }
 
 This is for 2d, if you add a tag to objects that are the ground they have to collide with them to be able to jump again.
 
 if (coll.gameObject.tag == "Ground"){
         notAirbourne = true;
         }
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
avatar image
0

Answer by OP_toss · Dec 21, 2013 at 12:42 AM

You could manually check the speed of the player by storing the previous position from the last frame (speed = (currentPos - prevPos)/Time.deltaTime). But I don't think this is the best test for jumping.

What if you were walking up a slope? Now suddenly, you can't jump while walking upwards, or downwards for that matter.

Rather, I suggest you have a bool, or better yet, a state variable that you set when you begin a jump, and revert when you end a jump. Then you can check against the state to see if they can or cannot jump. States can be as simple as an enum, or you can build a state machine if you want to get fancy. But they are the best way to do this type of thing, and will make future complexity much nicer to deal with.

For instance, what if you want to allow double jumps? Simply add a state for JUMPING, and another for DOUBLEJUMPING, and check that the state is either WALKING, STANDING, or JUMPING in order to permit a jump. EZ!

Hope this helped!

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
avatar image
0

Answer by satyambhatt20 · May 26 at 01:57 PM

alt text


1.png (78.1 kB)
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

21 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

Related Questions

Character Won't Jump 2 Answers

How fluent do I need to be in JavaScript before taking a crack at Unity's tutorials? 3 Answers

Greater than and less than syntax problem 1 Answer

New to Unity and stuck!!! 1 Answer

Beginner Car Control Script from FlatTutorials 3 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