Need to make grid movement more smooth
I am building a top down grid style game where the player is controlled by the arrow keys and one key press in a direction moves the character one block in that direction. The code I have now works fine for getting the actual result, but I would like the transition from block to block to be more smooth, so instead of just teleporting from one block to the next I would like it to kinda slide into the next block.
Here is my code
public float timeBetweenMoves = 0.3333f;
private float timestamp;
void Update () {
ArrowKeyMovement();
}
void ArrowKeyMovement()
{
if (Time.time >= timestamp)
{
if (Input.GetKey("up"))
{
transform.position = new Vector3(transform.position.x, transform.position.y + 1);
transform.eulerAngles = new Vector3(0, 0, 0);
timestamp = Time.time + timeBetweenMoves;
}
else if (Input.GetKey("down"))
{
transform.position = new Vector3(transform.position.x, transform.position.y - 1);
transform.eulerAngles = new Vector3(0, 0, 180);
timestamp = Time.time + timeBetweenMoves;
}
else if (Input.GetKey("left"))
{
transform.position = new Vector3(transform.position.x - 1, transform.position.y);
transform.eulerAngles = new Vector3(0, 0, 90);
timestamp = Time.time + timeBetweenMoves;
}
else if (Input.GetKey("right"))
{
transform.position = new Vector3(transform.position.x + 1, transform.position.y);
transform.eulerAngles = new Vector3(0, 0, 270);
timestamp = Time.time + timeBetweenMoves;
}
}
}
I have tried things like this, but they don't fix the problem.
if (Input.GetKey("up"))
{
//transform.position = new Vector3(transform.position.x, transform.position.y + 1);
for (int i = 0; i < 50; i++)
{
transform.position = new Vector3(transform.position.x, transform.position.y + .02f);
}
transform.eulerAngles = new Vector3(0, 0, 0);
timestamp = Time.time + timeBetweenMoves;
}
Thanks for any help!
Answer by KoenigX3 · Jan 05, 2017 at 12:34 AM
You need to use the Vector3.Lerp function, which stands for linear interpolation.
First, declare a new Vector3 variable, named as desiredPosition. At each Input.GetKey method this Vector3 will be updated with new coordinates instead of changing the position of the gameobject.
After handling keyboard input, you can change the position of the gameobject with the Vector3.Lerp function. The method will smoothly translate the gameobject from the initial position to the desired position. The speed can be changed by the interpolationSpeed value.
You can also interpolate the eulerAngles of the transform with the same method. Be sure to use a new Vector3 variable and update it each time you press a movement button.
public float timeBetweenMoves = 0.3333f;
private float timestamp;
public float interpolationSpeed = 10.0F;
Vector3 desiredPosition;
void Start()
{
desiredPosition = transform.position;
}
void Update ()
{
ArrowKeyMovement();
transform.position = Vector3.Lerp(transform.position, desiredPosition, interpolationSpeed * Time.deltaTime);
}
void ArrowKeyMovement()
{
if (Time.time >= timestamp)
{
if (Input.GetKey("up"))
{
desiredPosition += Vector3.up;
transform.eulerAngles = new Vector3(0, 0, 0);
timestamp = Time.time + timeBetweenMoves;
}
else if (Input.GetKey("down"))
{
desiredPosition -= Vector3.up;
transform.eulerAngles = new Vector3(0, 0, 180);
timestamp = Time.time + timeBetweenMoves;
}
else if (Input.GetKey("left"))
{
desiredPosition += Vector3.left;
transform.eulerAngles = new Vector3(0, 0, 90);
timestamp = Time.time + timeBetweenMoves;
}
else if (Input.GetKey("right"))
{
desiredPosition += Vector3.right;
transform.eulerAngles = new Vector3(0, 0, 270);
timestamp = Time.time + timeBetweenMoves;
}
}
}
Your answer
Follow this Question
Related Questions
The object moves more that what I want (MoveTowards) 0 Answers
Grid Movement with Smooth Movement 0 Answers
How can I make the character jump between the boxes? 0 Answers
Is ther a way to base movement translation through the grid map of the tilemap? 1 Answer
Raycast is hitting enemy even when not in contact with it? 2 Answers