Player setup and simple movement

It’s time to start prototyping! My goal today is to create my ‘Player’ and let them move.

I plan to eventually map the WASD keys to moving the ‘Player’ object around the play space, but I want to ensure the player starts at the same place every time and that I understand the concepts of movement in general.

I’m going to create a Scripts folder and create a new C# script named Player, then link it to the cube object.

Within the script, I’m going to utilize the ‘transform’ property of the cube object in order to set a start position of 0 on X, Y, and Z. So I’m going to go to void Start() and write transform.position = new Vector3(0, 0, 0);.

This will ensure the ‘Player’ starts at this position every time the program starts or restarts.

Next, I’m going to use transform.Translate in order to add simple, unguided movement for now. Under void Update, I’ll write transform.Translate(new Vector3(1, 0, 0) to move the cube to the right.

Obviously, this is WAY too fast.

I need to introduce real time to the phrase to keep it on the screen. So I’ll take the same code and multiply Time.deltaTime to change it from one unit per frame, it’s instead one unit per second… But this will be too slow, so I’m going to multiply the Vector3 by 5 first.

This is much better as it’s not a crawl, but not disappearing entirely off the screen.

That’s it for now, but next I’ll be taking a look at variables to change speed without having to go into the code each time.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store