Better movement (noob)

Hey guys super new to unity, just started yesterday and i’m teaching myself the C# language but it’s a slow process. I figured out a way to move my character using simple transform commands, but the movement is super jolty. Any chance someone could recommend a better function for smoother movement? This is my current setup. Thanks guys =)

if (Input.GetKeyDown(KeyCode.LeftArrow))
		{

			Vector3 position = this.transform.position;
			position.x--;
			this.transform.position = position;
		}

you need to use the

gameObject.transform.Translate(position );

Your way of doing it is generally not the way to go in unity.

The unity way of moving based on input from e.g. keyboard (not restricted to):

This is just one way of doing it (very simple as you never mentioned what kind of motion you want - e.g. 2d topdown, 2d sidescroller, 3d character controller etc.

float moveSpeed = 5f;

Vector3 deltaPos = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

transform.position += deltaPos * moveSpeed;

Also you should try to have a look at the standard character controller provided by unity. You find it in the standard asset pack

hope this helps :slight_smile:

It really depends on what kind of game you’re doing. But its best to add a float variable for your speed.

Then do this

//Because you used KeyCode.Left, I'll show you going left.
//The same goes for the other directions too.
//just change Vector3.left to Vector3.up, Vector3.down or Vector3.right.
//depending on the direction you want to use.
transform.position += Vector3.left * speed * Time.deltaTime;

The only problem with doing things this way is the collisions can be a little buggy, but seeing as you’re new, this is a good way to start and learn how to deal with vectors, which WILL save you a lot of hassle later on.

Sorry I forgot to mention, i’m looking for help in regard to a 2D sidescroller. Thanks for the tips guys, and i’ll have a look into the character controller and check out your function suggestions. =)

You need to look into the ‘Update’ and ‘FixedUpdate’ functions for smoother movement.

When I was learning I used the following tutorial to help me:

2D Character Controllers

Skip to around 40 mins for the code, but the whole video is worth a watch if you’re a beginner.