Specialization / Portfolio Piece
Timeframe: 4 weeks (half time)
A fast swing mechanic in a big city. Made as a PC sandbox with controller support.
For our last individual course at The Game Assembly, we got the opportunity to create something and learn more about a specific subject over a four week period. Seeing as it was completely free of choice, there were endless opportunities and things I could do - making it quite difficult to choose. I knew I wanted to do something gameplay related - but what?
The one idea that stuck by to the end was to experiment with one of the coolest game mechanics I've seen: the swinging from Spider-Man (PS4/PS5).
My goal for this project was to create a 3rd person controller with a swing mechanic as the core of the movement. This would also allow me to dabble in physics based movement and doing some camera work.
I chose Unity as the engine this project would be based on, which allowed me to start prototyping right away as I had some earlier experience with the program.
The first thing I needed was a simple character controller that could move around in the world, as well as a camera following you around. After implementing some basic movement, I started working on the core of the project - the swinging mechanic.
Before starting the course, I thought a lot about how you could implement swinging in a game. My beginning thought was to rotate the player around a fixed Y-axis at an anchor position. This would allow the player to more or less 'orbit' the swing-point, creating a motion similar to that of a pendulum.
Drawing inspiration from the reference
In a video from GDC, Insomniac Games go through how they created the movement and traversal in Spider-Man. This video came to be a great source to base my own project on, as they explain many of the problems they encountered when creating the movement, as well as how they solved them.
More and more of watching the reference game, I noticed how bad my first iteration of swinging was. There were many problems related to how I had first done my swinging. First of all, it had nothing to do with physics at all, which was one of the subjects I wanted to learn more about. Another problem was that it was difficult to find a good Y-axis to rotate around, as you can see in the video above where the player rotates more to the side than forward. A third problem was that it was way too rigid for a mechanic based all on fast paced movement and quick direction changes.
Because of this, I started to rework the swinging from the ground up to make it better and more easy to work with.
Iteration and looking into the pendulum motion
So the main focus of the rework was to create better, more fluid swinging. I also took the time to do redo some of the base movement as well. Now all of the movement was based on Unity's rigidbody system where I am directly working with my character's velocity-variable in code. This gave me a lot of control of how the player moves around.
Now for the swinging, I had to rethink everything about it and how I would be able to implement it based on physics. Thankfully, Insomniac Games bring some of their formulas of creating the swing into light - a pendulum motion. So I started looking into how pendulums work, reading a lot of math I hadn't worked with before. It was quite a challenge at first, but I tried over and over again with different variations for finding the correct force to apply to the velocity.
Then I was able to recreate half of the
motion of a pendulum. This was a very
big relief for me as I had finally made
some real progress on the swing!
By projecting the gravitational force onto L (the vector between the anchor point and the player), we can find the perpendicular gravity vector by subtracting G from G-Parallell. This G-Perp is then what in my case is creating the pendulum motion and what I set my velocity to.
Now I almost had it! The only thing left is to create a full motion.
The way I'm doing it is quite simple actually. I'm doing the same kind of math as before, but just changing the direction G points to mid swing. So when we "reach" the normal gravity vector, we simply redirect it to the intended end point of the swing. This then creates a pendulum-like motion.
The swing in action
By being able to manipulate where the G-vector points to, it was easy to make the swing more controllable and easy to use. You can impact the direction mid-swing by turning for even more control.
Moving onto world-based swinging
So far, all of the swinging has been simulated outside of the world with no dependency on buildings and the environment around you. This was a choice I made early on because I wanted to focus on creating a nice swing and getting the core of the swing right. However, I always had it in the back of my head to implement some type of in-world swinging, since that makes it more authentic in a way. I also got some feedback that some world-based swinging was what I was missing. Seeing as I had some time left, I spent the last time of the project to create at least a simple version that allowed the player to swing on actual buildings. This version is based on raycasting in the direction of the swing, in other words either left or right in a cone-like shape.
This really added to the whole experience, even though it is far from perfect.
Juicing and even more juicing.
No gameplay is too interesting if it isn't juiced in any way, at least if you ask me. Therefore I worked a lot on improving the general feel of the swing and the speed/momentum build-up to create the feeling of a fast, versatile swinging mechanic that provides fluid movement.
I also put in a lot of work into the camera, since it can be a great way to affect the feel of the gameplay without actually affecting any of the gameplay's logic. One of the most important things with the camera was to amplify the feeling of speed buildup in the game. I am doing this with a combination of moving the camera further away from the player, as well as increasing the FoV the higher your speed gets. By doing this, you can really feel the buildings 'swoosh' past you when you're swinging around and it honestly feels great.
I also added a bit of a roll to the camera when you're turning and changing directions mid-air. This helped to remove a lot of the rigidness of the camera that I still felt was present even with the camera work that was already there.
Then lastly I put some time into making the character less visually stiff, by finding some free animations that could add to and enhance the feedback of the gameplay.
The Final Result
In conclusion, the swing is not completely based on real physics and a lot of things are faked to replicate the behaviour of physics. This is partly done for simplicity, but also to give more control of how the player interacts with the swinging.
For example: changing direction mid-swing. This is not something you would really be able to do in real life. However, this behaviour gives way more control to the player, deciding where you want to go with your swing (plus it just feels nice).
Some speed buildup
If I had more time I could take this even further - making the swing even better, finding a good solution to in-world swinging, adding even more movement options and so on. However, I am happy with what I managed to accomplish in this rather limited time-frame. I did what I set out to do: experimenting with physics-based movement!
This project has been a blast to work on and I have multiple times needed to remind myself to stop playing around in the play-mode and jump back on working on the code. I am really pleased with the feel of the swing and I have learnt a lot about 3rd person controllers, game feel and game physics.