- Home /
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 =)
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.
Looks good but remember that the term "simple math problem" might not apply to everyone ;)
It looks complicated, but read a bit more about ballistic, it's simple math that translates very well to Unity.
Hello Ceandros! Sorry for the delay to give you feedback! I'll test and let you know if everything worked fine.
Thanks in advance! =)
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!