- Home /
Question by
munggol97 · Nov 08, 2018 at 12:16 PM ·
movementgameobjectvectorcharacter movementsmoothly
How to change game object position smoothly not snapping to the position
I have character for my game that changes its position with one click, my problem is when it changes its position it is just snapping to the position, I want some smooth movement of my character going to that position like having some rotation to the direction where it is going. here is my current code:
if (Input.GetMouseButtonDown(0))
{
firstPos = new Vector3(1.65f, -2.8f, 0);
SecPos = new Vector3(-1.65f, -2.8f, 0);
if (transform.position == firstPos)
{
targetPos = new Vector2(transform.position.x + pos1, transform.position.y);
transform.position = Vector2.Lerp(transform.position, targetPos, speed * Time.deltaTime);
}
else if(transform.position == SecPos)
{
targetPos = new Vector2(transform.position.x - pos1, transform.position.y);
transform.position = Vector2.Lerp(transform.position, targetPos, speed * Time.deltaTime);
}
}
Comment
Answer by dan_wipf · Nov 08, 2018 at 12:51 PM
What you could do is to change the interpolation t of Vector2.Lerp(vector2,vector2, t(Range 0 - 1) by Mathf.Smoothstep.
in your case this would be for example:
transform.position = Vector2.Lerp(transform.position, targetPos, Mathf.Smoothstep(0f ,1f , speed * Time.deltaTime));