Code Cleaner

Cliff Bailey
Jul 6, 2022

Let’s do more of that optimisation.

First, let’s create a new method, just for player movement.

Then, cut and paste everything from void Update() into void CalculateMovement().

Bear in mind, void Update is automatically called every frame, so CalculateMovement needs to be called from Update.

Simply put CalculateMovement(); into Update and there you go!

Now the Update function is cleaned up and ready for more.

Below is what your code should look like now (and you can delete the pseudo code).

But wait! THERE’S MORE!

We gotta…CLAMP it down.

clamping

This clamps the given value between given minimum and maximum float values and then returns a given value if it is within the minimum and maximum range.

Here’s what the new code looks like (and note, it’s only for the y-axis).

If you did this for the x-axis then that kills the wrap effect.

--

--

Cliff Bailey

Software Developer w/ADHD learning and doing life.