Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 jonasca · Oct 04, 2019 at 04:49 PM · physicsmathgameplaymechanics

Hop (Ketchapp) Mechanic (Jump between platforms with certain velocity while controlling object on X axis)

Hello guys! How are you?

I having trouble to create a mechanic like Hop from Ketchapp (https://apps.apple.com/us/app/hop/id1154436120), where I can freely change the platforms distance and ball jump height while the ball itself can still calculate how fast she has to go to reach the next platform exactly.

I've tried some codes already of my own, but it was a mess and very hardcoded to workly correct (a lot of magical numbers and somethings still broken).

I've tried to use DOTween as well, but since I can't ignore one axis, it didn't work.

I need the ball to calculate the z velocity based on an impulse on y-axis (how high it will jump) and a distance on z axis (distance between two platforms), while I can still move the player throw the x-axis. Any thread, video or info on the topic will be very helpful =)

Thanks in advance for the help =)

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
1
Best Answer

Answer by lgarczyn · Oct 04, 2019 at 07:27 PM

It's a pretty simple math problem.

You basically want your ball position to do three things:

  * z increases linearly
  * x follows user input
  * y follows a parabola

There are plenty of ways to do that in Unity:

  • The first solution would be a rigidbody with gravity enabled, making the physics material drag 0 and bounciness 1, and calculating the vertical velocity of your parabola using a ballistic calculator. The problem is that any error would accumulate, and you would risk missing the platform after a while. You would also have the Unity usual problem of the ball's motion being jerky because the fixed framerate is different than the render framerate.

    • You could also implement a basic physics engine for your ball on Update instead of FixedUpdate, by reducing the y velocity by gravity every frame, and adding the velocity to the position every frame too. This solves the second problem, but not the first.

    • Finally a simple mathematical solution:

       float gameTime = 0f;
         float timeSinceLastBounce = 0f;
        
         public float bounceDistance = 1f;
         public float horizontalVelocity = 2f;
         public float verticalVelocity = 2f;
        
         void Update()
         {
             // We calculate the position
             Vector3 position;
        
             // Your input code, I suggest looking up Mathf.SmoothDamp for smoothing
             //position.x =
        
             // The ball keeps the same horizontal velocity
             position.z = gameTime * horizontalVelocity;
        
             // The time between two bounces
             float bounceDuration = bounceDistance / horizontalVelocity;
             // The time between a bounce an its highest point
             float halfBounceDur = bounceDuration / 2f;
        
             // Gravity needs to cancel completely the verticalVelocity at the highest point of the curve
             // verticalVelocity * halfBounceDur == 0.5 * gravity * halfBounceDur * halfBounceDur
             // therefore
             float halfGravity = verticalVelocity / halfBounceDur;
        
             position.y = verticalVelocity * timeSinceLastBounce - halfGravity * timeSinceLastBounce * timeSinceLastBounce;
        
             transform.localPosition = position;
        
             // We update the time counters
             gameTime += Time.deltaTime;
             timeSinceLastBounce += Time.deltaTime;
        
             // We check if we reached a bounce
             if (timeSinceLastBounce >= bounceDuration)
             {
                 // We check if we hit a platform
                 if (Physics.Raycast(position, Vector3.down))
                 {
                     // Bounce success, play sound?
                     timeSinceLastBounce -= bounceDuration;
                 }
                 else
                 {
                     // Game lost
                 }
             }
         }
      
      
      

All math in this code is deduced from the equation newPosition = oldPosition + oldVelocity * time + 0.5 * gravity * time²

Note that bounceDuration, gravity, etc. could be calculated in Start, as they are always the same. However calculating them here allows you to tweak other parameters in-game.

I have not tested this, so be free to tell me if it tell me if it doesn't work. It should though.

Comment
Add comment · Show 4 · 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 Captain_Pineapple · Oct 04, 2019 at 08:43 PM 0
Share

Looks good but remember that the term "simple math problem" might not apply to everyone ;)

avatar image lgarczyn Captain_Pineapple · Oct 04, 2019 at 09:35 PM 0
Share

It looks complicated, but read a bit more about ballistic, it's simple math that translates very well to Unity.

avatar image jonasca · Oct 09, 2019 at 07:07 PM 0
Share

Hello Ceandros! Sorry for the delay to give you feedback! I'll test and let you know if everything worked fine.

Thanks in advance! =)

avatar image jonasca · Oct 13, 2019 at 09:44 PM 1
Share

Ceandros, it didn't work not because your code, but for the way other things were working on my game.

Thanks by the way! I solved in another way here (not going to post because it's linked on other things to the game, It'd be very confusing I think).

Thanks again!

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

189 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 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 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 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 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 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 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 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 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

Multiple Cars not working 1 Answer

Convert from m/s to m/fixedFrame 1 Answer

Is there a good approach to implementing battle damage to clothes? 1 Answer

Bounce rigidbody backwards without knowing its rotation 1 Answer

2.5D Platformer where character moves between points has issues where physics take player off of the line between points. 0 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