Day 5: Working with different speeds of our player in our 2D Development Project

Alex Frankovic
3 min readAug 15, 2022

--

Objective: How do we distinguish between different speeds in the characteristics of our player in our unity project?

So we have built our first C# script and applied it to our player in our project. In that script we have made it move from the center of our game to the right (Vector3.right) or to the left (Vector3.left). But when we did this in our previous article, our player just zooms off into that direction without even looking back. So how do we change the characteristics of this speedy guy?

Before we get into that we want to take note that:

  • Vector3.right is equivalent to Vector3(1, 0, 0)
  • Vector3.left is equivalent to Vector3(-1, 0, 0)

Now that we know this, we are working towards the next portion of our script which is labelled void Update. What “Update” means is that our game will update once per frame which would typically be about 60 frames per second.

Next step in our C# Script; anything happening after starting point

So with this being said, in order to slow our player down and not have him run crazy off our game, we want to incorporate real life time. In order to do this we want to multiply our Vector3 with what is called Time.deltaTime. What this does is it brings our frame rate to real life minutes and seconds. In basic terms, Time.deltaTime is equivalent to one second.

Adding Time.deltaTime to our C# Script

After incorperating real time into our project and multiplying it by our Vector3.right, we can go ahead and apply it to our project, we can see that our player is now moving at a slower rate. This is moving at 1 meter per second.

Player moving at 1 meter per second.

So, if we were to have our player to move just a bit faster; we would want to incorporate a faster pace. So let’s say we want our player to move 6 meters per second. We would want to add in (Vector3.right * 6 * Time.deltaTime).

Adding in a faster speed for our player

After compiling the script and adding it to our unity project, you will now see that our player is moving faster at 6 meters per second.

Player moving at 6 meters per second

Depending on how fast we want our player to move, we can adjust how many meters per second we want it to move and make sure we are multiplying it by our Vector3 and real time which is Time.deltaTime.

--

--