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 DjRaffa93 · Oct 05, 2013 at 04:59 AM · javascriptmovementgravityjump

Particular "Jump" Script.

Hello! I have a problem with my script, I have not found anything that can help me that has already been asked. This is my script jump, I would like it if my finger is on the button the player starts to rise high, and when I release it back down, Like Jetpack JoyRide.

Any Ideas?

 function Update()
 {
     var movement = Vector3.zero;
     MainCharacter.animation.CrossFade ("Run",2);
 
     // Apply movement from move joystick
     if ( moveTouchPad.position.x > 0 )
         movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
     else
         movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
     
     // Check for jump
     if ( character.isGrounded )
     {        
         var jump = false;
         var touchPad = jumpTouchPad;
             
         if ( !touchPad.IsFingerDown() )
             canJump = true;
         
          if ( canJump && touchPad.IsFingerDown() )
          {
             jump = true;
             canJump = false;
          }    
         
         if ( jump )
         {
             // Apply the current movement to launch velocity        
             velocity = character.velocity;
             velocity.y = jumpSpeed;
         }
     }
     else
     {            
         // Apply gravity to our velocity to diminish it over time
         velocity.y += Physics.gravity.y * Time.deltaTime;
                 
         // Adjust additional movement while in-air
         MainCharacter.animation.CrossFade ("Jump");
         
         movement.x *= inAirMultiplier;
 //        movement.z *= inAirMultiplier;
     }
         
     movement += velocity;    
     movement += Physics.gravity;
     movement *= Time.deltaTime;
     
     // Actually move the character    
     character.Move( movement );
     
     if ( character.isGrounded )
         // Remove any persistent velocity after landing    
         velocity = Vector3.zero;
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Oct 05, 2013 at 04:12 PM

I can tell you why you are having a problem, but I it will take some work on your part to figure out a fix. Right now you set the velocity for a jump on line 31. This is inside of if 'character.isGrounded', which means that once the jump is started, that line is not executed. You need one of two things to happen. Either you need to have the 'y' velocity set as long as the finger is held on the touchPad, or you need to suspend gravity while the finger is held on the touchPad. I would try first replacing line 47 with:

 if (!jumpTouchPad.IsFingerDown()) {
     movement += Physics.gravity;
 }

I suspect there will be other changes, but this is a possible first step.

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 YoungDeveloper · Oct 05, 2013 at 05:07 AM

Create a button. On button press increase transform.localPosition.y plus some float * Time.deltaTime. You should also have two variables which will represent maxheight and ground base position, so you know much can you lower or higher the character. You lower the character same way you higher it but with minus.

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 DjRaffa93 · Oct 05, 2013 at 03:26 PM 0
Share

i try this but isn't work.

player.transform.localPosition.y += force * Time.deltaTime;

avatar image YoungDeveloper · Oct 05, 2013 at 04:13 PM 0
Share

Did you even did some research before asking or you just tried to write what i said ? You have to understand what exactly you want to move, if that object is parented or not, that is should you use localpos.. or just position.

Something like this, idea itself.

 float maxHeight;
 
 void Start(){
  maxHeight = transform.position.y;
  maxHeight += 5f;
 }
 
 Update(){
 //if button pressed{
    Vector3 currHeight = transform.position;
 
    currHeight.y += 1f * Time.deltaTime;
    if(currHeight.y > maxHeight) currHeight.y = maxHeight;
    transform.position = currHeight;
 }
 }

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Player object falling slowly when holding input 2 Answers

Make Lerp or other more fluid or continuous 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