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 undefeated_monarch · Mar 01, 2015 at 01:38 AM · javascriptplayertouchjumpcontrols

When I Hold The Jump Button, Player Will Fly?

So, I'm having a bit of an annoying issue right now. I am using EasyTouch for touch screen controls BTW.

One of the touch controls is a button. When you press the button, it sends a message. Then, in my Player Jump script, if the message is received, the player will jump. In simpler terms, when I press the button, the player jumps. If I press the button then release. The player jumps like he should. However, if I hold the jump button, the player will sort of fly? He'll sort of go straight upwards, almost like an infinite jump...

 var JumpHeight = 8;
 var PlayerJump : boolean = false;
 
 //Message received will result in this function being "activated"
 function Jump ()
 {
     PlayerJump = true;
 }
 
 function Update ()
 {
     if (PlayerJump == true)
     {
         rigidbody.velocity.y = JumpHeight;
         //My attempt at making the player not fly straight up      //into the air...
         PlayerJump = false;
     }
 }

By the way, I'm brand new to Unity, so easy to understand explanations please ;)

Thanks everyone! :D

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 undefeated_monarch · Mar 01, 2015 at 05:29 AM 0
Share

Guys, please, don't argue, I appreciate any responses! ;D

BTW I managed to fix it. I'll try to remember to post my fix when I get home for anyone else who may need it in the future! :)

Thanks for the help! :D

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by shay4545 · Mar 01, 2015 at 03:22 AM

That is because the Update function is called every frame, so your code basically says, if the jump button is being pressed, add some force making the player go up. Setting PlayerJump to false isn't working because every frame the jump button is held down, you're adding more velocity to the player. You would first have to check if the player is grounded and only add the velocity if that is true

Make an empty Gameobject called mytrans and another called groundCheck. Make them children of your player. Replace your script with the code below and assign mytrans to the myTrans variable in the inspector and make groundCheck the groundTag variable in the inspector. Position myTrans at 0,0,0 or the center of the player and make groundCheck a little bit below the players feet. That should work, if it doesn't you can comment below.

I edited the code so that the boolean isGrounded is set to the return value of a linecast. this means that whenever the line that is drawn from mytrans to groundCheck is hitting something, the value returns true, making the isGrounded variable true. Then when the jump button is pressed and the isGrounded variable is true, your player will jump.

  var JumpHeight = 8;
 var PlayerJump : boolean = false;
 var isGrounded : boolean;
 var myTrans : Transform;
 var groundTag : Transform;
 //Message received will result in this function being "activated"
 function Jump ()
 {
 PlayerJump = true;
 }
 function Update ()
 {
 isGrounded = Physics2D.Linecast (myTrans.position, groundTag.position);
 if (PlayerJump == true && isGrounded)
 {
 rigidbody.velocity.y = JumpHeight;
 }
 }
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 undefeated_monarch · Mar 01, 2015 at 09:25 PM

Okay so I fixed my issue. I temporarily made another script for the jumping exclusively.

 static var Jump : boolean = false;
 
 var JumpHeight = 8;
 var DistToGround : float;
 
 function Start ()
 {
     DistToGround = collider.bounds.extents.y;
 }
 
 function MoveJump ()
 {
     Jump = true;
 }
 
 function Update ()
 {
     if (Jump == true && IsGrounded ())
     {
         rigidbody.velocity.y = JumpHeight;
         JumpFalse ();
     }
 }
 
 function IsGrounded ()
 {
     return Physics.Raycast(transform.position, -Vector3.up, DistToGround + 0.1);
 }
 
 function JumpFalse ()
 {
     yield WaitForSeconds (1);
     Jump = false;
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Jump Further When Key Is Held Down 1 Answer

Jumping not allways registering fix? 0 Answers

how make this script follow a target. 1 Answer

Getting raycast to fire at touch position but only when second finger is on screen 0 Answers

Mecanim tutorial issue with jump 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