0.1.0 - Player Movement (Week 8)


Don't Let Them Land v0.1.0

Going Round and Round

Moving the player around the planet is probably the most important and difficult thing to get right. It needs to feel good for the player so that they feel in control and can defend the planet effectively. So I knew there were a few ways to try this. Initially, I wanted to use a Point Effector on the planet and let Unity take care of all the maths, but quickly found that alone it wouldn't be enough. So, while a simple solution, it would result in it not feeling good and still require custom code to get it to align correctly to the planet itself (see Figure 1 for my Point Effector test).

Broken Movement

Figure 1 - Point Effector attempt

Thinking Long Term

So quickly throwing together some basic jumping and side-to-side movement on a Point Effector wasn't going to cut it. Since I needed custom rotation to keep the player aligned to the planet correctly, I might as well do custom gravity for it too, so I have the most control over things when things get more complicated later on. I would need gravity on lots of objects later, so I started by adding a script that defines the planet as a gravity attractor. Later, there might be other things that need gravity on it, but for now, it'll just be the planet.  It simply defines how much gravity the object has and provides a function to apply that gravity to another object that has a Rigidbody2D.

Next we'll build a script that allows an object (in this case, just the player for now) to be affected by this gravity. Simply, these objects just call the function on the attractor script for this rigidbody. They also have an option to align the body to the planet's surface. This adds some extra steps after to ensure the object (again, in this case the player, but later probably the UFOS too) remains aligned to the surface of the planet, which keeps things looking right and prevents spinning like above.

Lastly, we need a script to move the player itself. I wanted side to side and a jump. The jump is pretty simple, mostly. Just add force in the opposite direction of the planet from the player only when the player is touching the planet (so we don't jump into infinity). I used a small raycast to detect if the player is grounded instead of actual contact, since that felt a bit better to jump around with, as it gave a little leeway for the player to be considered grounded. Moving the player side to side was a little trickier. I needed to find the tangent of the player to the planet and then apply movement based on the direction axis the player was choosing. The maths got a lot more complicated than I wanted it to be, but it was worth it, and I'm really glad it feels good since I think getting this part to work well was going to be one of the most challenging parts of this project. See Figure 2 for the final product (or play it yourself :D).

Working Movement

Figure 2 - Working movement with sprite flipping

Watching From Above

The next important thing was to get the camera feeling good. Attaching the Main Camera to the player works fine, but it felt stiff and wouldn't allow for more complicated stuff later on. I wanted to start a little with Cinemachine now, so I had a basis to work off of.

Even though simply having the target of the Cinemachine camera be the player would have been sufficient for this update, I wanted to get a target group in now since that will likely be what I need later on when enemies actually exist (you need to see what you need to shoot after all). It's very subtle, but the camera is attempting to keep the UFOS in shot as seen in Figure 3. I'll definitely need to adjust these things later when the enemies are actually in and we can shoot them.

Cinemachine is very powerful, but lacks the kind of custom rotation I needed when using a target group (the only option is group average, which makes no sense). Perhaps there is a better solution, but for now, I added a custom script that rotated the Cinemachine camera with the player with some small amount of damping. More work on the camera is needed later when I start instantiating enemies that need to be added to the target group.

Target group settings

Figure 3 - The weights will be adjusted as the UFOS get closer to the planet but for now are just static

Feedback

While I wasn't able to attend the tutorial to get feedback from students this week, I did manage to get some feedback from friends. They liked the movement and said it felt good. They commented on the lack of anything else to do, which was fair. They also felt the camera and screen felt zoomed in, and the window was too small. I'll aim to adjust these things for the next major update or perhaps a small in-between fix.

References

Assets used:

Leave a comment

Log in with itch.io to leave a comment.