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;
}
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.
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. =)