- Home /
Moving an object by few units up and stopping it , but so like it looks like an animation .
I want to make a platform appear and make it look like it was animated . I dont know how to make Transform.Translate transition to position because in my game it just appears in the final position instead of ''traveling'' to final position.
Answer by Legend_Bacon · Aug 30, 2018 at 04:24 PM
Hello there,
If you want to actually use animations, This Tutorial should tell you everything you need.
If you want to animate through code, you're going to want to use Lerps and smoothing methods. Here's a good read.
Hope that helps!
Cheers,
~LegendBacon
Is it possible to use transform.Translate ins$$anonymous$$d of vector because i will attach this script to multiple objects in the scene and i dont want to set end positions to all of them or im just misunderstanding Lerps and Vectors ? I just want to make translate look like it moves to the spot not teleports there but i have tried vectors and they wont stop unless i set a endPos.
You can calculate the end position based on the Transforms current position. If you want to move two units to the right you would say transform.position + (Vector3.Right * 2)
Answer by TachyonBlast · Aug 30, 2018 at 07:08 PM
Alternatively, if you want a really fast solution (@Legend_Bacon 's script method is still better) you could just multiply the vector by Time.deltaTime, which will make the motion last for 1 second (and multiply by another number as a speed modifier). This won't work if it's not in Update/Coroutine, because if you call this only once you would simply move the object a very small amount.
Answer by hamsterbytedev · Aug 30, 2018 at 09:23 PM
You'll want to use Vector3.Slerp() or Vector3.Lerp()
Here's some untested code:
public float speed = 1;
public void Move(Vector3 vector) {
StartCoroutine(MovePlatform(vector));
}
private IEnumerator MovePlatform(Vector3 vector) {
float delta = 0;
Vector3 startPosition = transform.position;
Vector3 endPosition = transform.position + vector;
while (transform.position != endPosition) {
delta += Time.deltaTime / speed;
transform.position = Vector3.Lerp(startPosition, endPosition, delta);
yield return new WaitForSeconds(Time.deltaTime);
}
yield return null;
}
Your answer
Follow this Question
Related Questions
transform.Translate not working in if-statement? 1 Answer
UI objects move at different speeds on different resolutions 1 Answer
Move terrain towards object? (illusion of moving forward) 1 Answer
Move two objects at same time as they are one single object 0 Answers
My object is translating with speed but does not stop... 1 Answer