The first thing I added was “spin” in the game for both top and back spin, as well as side spin. I had already took top/back spin into account before, but not very much. There also wasn’t a way to have the player do it in the game. I had to manually set the values in the Unity Editor. And, there was no consideration for top spin whatsoever.
First, I made a UI thing for the player to enter in their spin. I just straight copied how it looks in golf story, with a sprite of a golf ball and a red dot on the ball to represent the spin. If the dot is higher up on the ball, that will create top spin. Lower, back spin. To either side, side spin to that side. It looks like this in the game:
The player clicks and drags with their mouse where they want to set their spin.
I wanted the spin to affect the trajectory, with things like top spin creating a lower overall height and launch angle, back spin with a higher launch angle, side spin causes the flight to curve a certain direction, etc. This is shown in the drawn trajectory. The below image shows side spin causing the flight path to curve, as indicated by the trajectory shadow:
You can also see in the above image the word “iron” which is to tell the player what club they are using. I just added 3 basic clubs to the game (putter, iron, driver) and each has a few specified attributes such as max distance, max top/back/side spin, etc. Nothing too complicated right now.
The final thing I added to the game were environmental objects that the ball can hit and bounce off of. For this top down mode, I’m doing some “custom” physics and other things due to Unity2d’s physic system not being exactly what I want for this mode. So, collisions with objects during flight was no different and I had to do some custom stuff.
To start, the collider for the ball isn’t on the drawn ball sprite, but instead on the balls shadow that is on the “ground.” For any objects, such as a tree, I also made it’s collider be at the base of it on the ground:
So, then, the shadow moves along the flight path on the ground. If the shadow collides with an object on the ground, it will then check that object’s height value. If the ball has a height value higher than the object the shadow collided with, the ball continues on its flight. If the ball’s height is lower than the obstacle, then the ball will collide and bounce off. This check is made for every OnTriggerEnter2D for the obstacle.
The “bouncing off” is all done by me instead of any Unity physics. For the most part, “I made it up.” To calculate the bounce off the object, I do things like check how far along the ball is on its flight (is it moving up still? back down to earth?), the ball’s height at the collision, and its movement direction, among other things. I kind of then arbitrarily decided how far back to bounce and how fast based on calculations from those values.
Once those collisions worked to my liking, I wanted the drawn trajectory to detect those collisions as well, so that the trajectory is only drawn to its first collision. So, basically, as the trajectory is being draw, it casts a ray (or, rather, a CircleCast) from the previous point in the path to the current point, and sees if it hit an object. If yes, it did hit something, it then checks the height values. If the ball is higher, continue flying and drawing. If the ball is not higher, stop drawing the trajectory (and approximate the collision point to be the last point in the trajectory).
Here’s what it looks like in action.
I think it looks good! There are certainly still some funky stuff. Like the ball bounce pretty far back after looking like it went through a lot of tree branches/leaves. That shouldn’t be too hard to adjust. Add something like “hard” or “soft” categories for the obstacles so the ball bounces far if its a hard obstacle but doesn’t bounce much at all after hitting a “soft” obstacle. Whatever, that is all adjustable. What was important was making sure my idea for the collisions worked as I thought they would, and I think it’s looking good right now!
Some easier things I want to add is like a target type thing at the end of the trajectory, and then have the camera focus on that target. Basically, copy Golf Story more. Also, maybe draw out some club sprites, create more just for fun. Maybe even design a real course? Ok, that’s a bit far down the line now…
Smell ya later, nerds!