AI For Games: Week 4

The Player

this week I’m going to be developing the player. It’s not an AI or anything related to the assignment so I’ll be brief in this post, although it will be a VERY big part of my game!

The Scene

Player The player will be made up of a KinematicBody (perfect for moving objects), the collision shape will be a pill (which is very standard for modern games), a Camera that will be following the player at all times and finally the 3D model (along with its animations).

The Script

I’m going to run through how I made the script in order, here we go:

Directional input

We’re going to store the direction the player is moving in a 3D Vector, 1 will be moving forward in that direction and -1 will be moving backwards, with 0 being not moving at all. When a key is pressed we set the Vector to its appropriate value for that axis, easy! When the key is released we have to make sure that the player is not moving in the other direction before we set the moving of that axis to 0! Now if we tell the player to move by that Vector every frame we can move around, it’s a bit fast and a bit jerky, so we’ll implement a velocity system next.

Velocity based movement

We start by making a 3D Vector, we’ll also make some constants:

  • Acceleration: the speed at which the player accelerates
  • Deceleration: the speed at which the player decelerates
  • max_speed: the maximum speed of the player Now, if the player is moving in a direction, we increase the velocity by acceleration until we hit the maximum speed, if it’s not we decrease it until it hits 0. We then move our player by our velocity multiplied by delta time, which will make the movement frame independent.

Animation

When the player is moving, set the animation to the moving animation, if not set it to the idle. We’ll also rotate the player to look at the direction they’re facing.

Damage

Finally, we have a function to call when the player is damaged. This could be anything, but I like the idea of the screen just going black when the player dies.




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Voronoi Graphics: Drawing shapes across voronoi edges
  • Markov Chains
  • AI For Games: Week 11
  • AI For Games: Week 10
  • AI For Games: Week 9