# Unity 5.5 with vertical movement and player size calculation

I’m working through the tutorials in Unity 5.5. Here’s my working code for player movement. I’ve added some enhancements because I find doing so helps me solidify what I have learnt. The extras in this code are:

• Vertical as well as horizontal movement.
• Instead of manually setting padding in the inspector, I grab the dimensions of the player sprite from the sprite renderer component (see playerWidth and playerHeight), halve them (transform position is dead centre of the sprite) and adjust the min/max values accordingly. I figure this will make it easier to swap graphics later on if I want to tart up the look of the game.

2 Likes

I will try this later, thanks!

1 Like

So funny i was about to post my own question about this. The 0 and 1 were confusing me about how to do top and bottom. I had upmost, downmost, leftmost and rightmost and I was confused to the utmost

Also the code for the sprite boundary padding is so simple and perfect if you want to swap to larger or smaller ships without messing around with values.

Great work!

1 Like

I’m not completely sure how ur controller work diagonally. Poking my own way through this is what I initially thought

``````// Update is called once per frame
void Update () {

if (Input.GetKey (KeyCode.LeftArrow)) {
//this does the same thing
//transform.position += new Vector3 (-speed * Time.deltaTime, 0, 0);
transform.position += Vector3.left * speed * Time.deltaTime;
} else if (Input.GetKey (KeyCode.RightArrow)) {
//does the same thing
//transform.position += new Vector3 (speed * Time.deltaTime, 0, 0);
transform.position += Vector3.right * speed * Time.deltaTime;
} else if (Input.GetKey (KeyCode.UpArrow)) {
transform.position += Vector3.up * speed * Time.deltaTime;
} else if (Input.GetKey (KeyCode.DownArrow)) {
transform.position += Vector3.down * speed * Time.deltaTime;
}
//restricts Fighter to gamespace
//float newX = Mathf.Clamp(transform.postion.x, xmin, xmax);
//transform.position = new Vector3(newX, transform.position.y, transform.position.z);
Vector3 fighterPos = new Vector3 (0f, 0f, 0f);
fighterPos.x = Mathf.Clamp (transform.position.x, xmin, xmax);
fighterPos.y = Mathf.Clamp (transform.position.y, ymin, ymax);
this.transform.position = fighterPos;
}
``````

}

Which gave me ridged movement and only one keypress at a time. I’m wondering if i could somehow fashion my way to work like yours (saying this just so I can get more knowledge).

P.S. is it just a trick of the eye or is there acceleration also acceleration happening?

1 Like

I think there are a number of things at play here. I’m a beginner too, so hopefully if I get any of this wrong someone with more experience can post to correct my n00bishness.

Firstly, you are using Input.GetKey. This is a binary input, the key is either pressed down or it isn’t. Input.GetAxis is analogue and seems to be designed to work with gamepad/joystick input. When I press a key down that’s mapped to an axis, unity seems to smoothly ramp up the input value from 0 (stopped) to 1 (full speed). I think maybe this is smoothing the input somewhat for my code.

I think you also need to remove the “else” from your code. If both the left and the up keys are pressed, the code will detect the left keypress but then will bypass all the other keypresses. Effectively the code reads like this:
if left is pressed move left.
if left is not pressed and right is pressed, then move right.
if left and right are not pressed and up is pressed, then move up.
if left, right and up are not pressed and down is pressed, then move down.

Hope that helps.

1 Like