- Home /
How do I move my object slowly from its original position?
Hello everyone, I'm new to Unity and I'm having an issue I believe should be easy to solve but haven't been able to!
I'm working on an Android game, and when I'm done dragging an object with touch, I want it to go to its original position. This isn't a problem when I want it to go from Point A to Point B immediately, but I want my object to go back to Point A slowly, and smoothly.
This is the part I have issues with, inside the update function.
if (touch.phase == TouchPhase.Ended) {
if(!lockedObject && objMoved == true) {
transform.position = Vector2.Lerp(originalPos, transform.position, Time.deltaTime); //This moves my object to its original position instantly
GetComponent<AudioSource> ().PlayOneShot (myClip[1]);
}
}
Answer by Basher207 · Apr 13, 2015 at 07:05 PM
Thats because you have to swap original pos and transform.position. Currently you are telling it to go towards your position from the original position to your target, so its immediately going to your original position and slightly heading towards your old position (which after one frame is where the original position is) I suck at explaining but try it :D
Answer by WillNode · Apr 13, 2015 at 07:24 PM
maybe you need to take a look about SmoothDamp
private Vector3 velocity = Vector3.zero;
void Update() {
transform.position = Vector3.SmoothDamp(transform.position, originalPos, ref velocity, 2);
}