Generalizing movement

While the lack of collision detection is bugging me, I need to tackle one other matter first. My code for moving the player currently looks like this:


public class PlayerController : MonoBehaviour {

void Update () {
if (Input.GetKeyDown("right"))
transform.position += new Vector3(1, 0, 0);
else if (Input.GetKeyDown("left"))
transform.position += new Vector3(-1, 0, 0);
else if (Input.GetKeyDown("up"))
transform.position += new Vector3(0, 1, 0);
else if(Input.GetKeyDown("down"))
transform.position += new Vector3(0, -1, 0);
}
}

Depending on whichever arrow key is pressed, my player will move one unit in that direction. The problem is, instructions how to move is all written directly into the player’s script. It isn’t a big deal right now because its just a transform.position statement(one line of code), but I’m going to be adding stuff to check for collisions before movement and there is nothing really unique about how the player moves compared to anything else. At best its pointless to have the same bits of code in multiple spots and at worst it will become confusing and tedious to make any change to movement because I would need to make the change in more places than one.

So I’d rather have one function for movement to use for multiple different things.  In that case, how will I know that it works?

2018-07-10_1012

I redid the scenario from last time. I added an npc to the bottom right. I’m going to have him move around once every hundred or so frames. Then I’ll have the player move with the keyboard. If both move as expected, then the code does what I want it to do.

In my Java prototype I did that by making a parent class called Entity where the move function existed and was available for anything that needed to move. The solution here is essentially the same.


public abstract class Entity : MonoBehaviour {
protected void Move(Vector3 movement)
{
transform.position += movement;
}
}

Having the Entity class as an abstract class was advantageous in my Java prototype, so even though the one here doesn’t need to be abstract to work, I still went ahead and made it one. The code is not going to know (or care) that the movement is originally from keyboard inputs, so I’m just asking for the direction of movement.

Then all I need to do is change the class for Player to inherit from Entity, and make it use the new Move function. I decided to go one step farther and move all the Keyboard stuff onto a separate function which is called from update.

<blockquote>void KeyboardInput()
{
if (Input.GetKeyDown("up"))
Move(new Vector3(0, 1, 0));
else if (Input.GetKeyDown("down"))
Move(new Vector3(0, -1, 0));
else if (Input.GetKeyDown("left"))
Move(new Vector3(-1, 0, 0));
else if (Input.GetKeyDown("right"))
Move(new Vector3(1, 0, 0));
}</blockquote>

Lastly, I saved everything and hit the play button. The npc is running around in circles as expected, and my player moves around according to my commands.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s