Ball Mechanics


For this Jam I already had an idea in mind, I just wrapped the idea around the theme. The game is a side-ways top-down Tennis game that resembles Pong, but it uses a pseudo-3rd dimension.

The most obvious feature of the game is the 2D fake height of the ball. This was achieved by having the graphics of the ball move vertically independently of the main body, The main body moves in all 4 directions with a Vector2 called "groundVelocity", While the ball graphics y-position moves in accordance with the float "verticalVelocity" while also being displaced by the main body's position.

When you spawn a ball into the scene you call an initialization function that sets up the initial groundVelocity and verticalVelocity values, and in the Update function on the ball, you can move the ball according to the velocities you just sat-up.

void Update() {
    verticalVelocity += gravity * Time.deltaTime;
    bodyTransform.position += Vector3.up * verticalVelocity * Time.deltaTime;
    
    transform.position += groundVelocity.Vec2ToVec3() * Time.deltaTime;
}

"gravity" is a local variable of the ball, You can specify it by yourself. You can see above that the bodyTransform position (the graphics of the ball) increments in the y-axis by the verticalVelocity while the verticalVelocity variable is being incremented by the gravity over time so that the ball falls after the initial verticalVelocity is lost.

I also have the ball bounce off the ground and this is achieved by detecting whenever the bodyTransform.position.y equates or is less than transform.position.y . Whenever that condition is met I just invert the verticalVelocity.

if(bodyTransform.position.y <= transform.position.y)
{
    verticalVelocity *= -1.0f;
}

And when you put the 2 together you get a simplified version of the ball in PONG2.

Files

Pong2.zip Play in browser
Jan 15, 2023

Leave a comment

Log in with itch.io to leave a comment.