- Home /
Move gameobject from point a to point b smoothly
Hello there fellow unity community members, i've been trying to make a drawer in a dresser to move out (or to open) when interacted with, but when i tried animation the animation never played so now im using this code below to open the drawer. But it's opening in a blink of an eye, i want it to open realistic, like using lerp or something but i dont know how and all the other feeds about this question is really difficult to understand. Thanks
Goal: The drawer needs to be opened slowly or smooth instead of just teleporting.
#pragma strict
var OpenDrawer : OpenDrawerJS;
var OpenDrawerPosition : Transform;
function Start ()
{
OpenDrawer = GameObject.Find("Player").GetComponent(OpenDrawerJS);
}
function OpenDrawer23333 ()
{
Debug.Log("OpenDrawer");
gameObject.transform.position = OpenDrawerPosition.transform.position;
OpenDrawer.DrawerIsOpen = true;
}
Take a look at these functions:
Answer by ahaykal · Jul 14, 2016 at 12:49 PM
This question has been asked a lot from before:
You can use MoveTowards().
public float speed;
void Update() {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, OpenDrawerPosition.transform.position, step);
}
You have to give us more details, like what didn't it work? Do you have any errors? If you do then what are they?
You should provide which values you use for speed ins$$anonymous$$d of copying and pasting the documentation.
Answer by vhzhang2020 · May 25, 2020 at 09:06 PM
You should use IEnumerators (or are you using unity script?) example:
public Transform pointA;
public Transform pointB;
public float speed = 5f;
void Start()
{
transform.position = pointA.position;
StartCoroutine(move());
}
IEnumerator move()
{
while (transform.position != pointB.position)
{
Vector3.Lerp(transform.position, pointB.position, speed);
yield return new WaitForEndOfFrame();
}
}
Your answer
Follow this Question
Related Questions
Lerp Gameobjects.. 1 Answer
Use Vector Lerp or other to move back with arc 2 Answers
Move child gameobject in x axis (Lerp) 1 Answer
Opening and closing drawers with start and end points 1 Answer