- Home /
How to move an object smoothly while in a Coroutine / Ienumerator?
I know this question has been asked before, and I've tried almost everything. My object is traveling around a cylinder and I would like for it to move over a certain amount of space on the press of an arrow key. The problem I'm having is that it just jumps over to the other position, when I would like for the change to be gradual and for the object to move more smoothly. I'm using transform. translate, but I've tried all the other solutions I've found on the internet ( transform. position = etc.)
Help is greatly appreciated, thanks. (Also, I'm not sure if it says but I'm using c#.)
if (Input.GetKeyDown (KeyCode.RightArrow))
StartCoroutine (switchright ());
if (Input.GetKeyDown (KeyCode.LeftArrow))
StartCoroutine (switchleft ());
}
IEnumerator switchright () {
yield return new WaitForSeconds (1);
transform.Translate (Vector3.right * brem);
}
IEnumerator switchleft () {
yield return new WaitForSeconds (1);
transform.Translate (Vector3.right * berm );
}
Answer by teh1archon · Sep 08, 2016 at 12:46 PM
Dude, you've forgotten to factor in time. If "brem" is your speed then it should be: transform.Translate (Vector3.right berm Time.deltaTime);
Using a coroutine is no different in that matter than using it in Update.
Also, above code will still not work because in CoRo you need to make your own loop for this to move continuously. And you probably want to slerp the movement like here: https://docs.unity3d.com/ScriptReference/Vector3.Slerp.html
Thanks man , but when I use time.Deltatime its still choppy and it barely changes position. I also want to move it a fixed distance, not continuously , but I'll look into Vector3.slerp. Thanks
Answer by AurimasBlazulionis · Sep 08, 2016 at 08:50 AM
I would suggest not using coroutines, instead use Vector3.Lerp
inside update loop. When you press a button, store the position of the object and Time.time
in variables. Then, you also have time for the stuff to happen, for example 2.0, you can also store the value of it somewhere like float moveTime
so it can be changed. Then target position is how much you want to move the object. So the lerp function should look like this:
Vector3.Lerp(transform.position, transform.position + targetPosition, 1f / moveTime * time.deltaTime);
Just assign position of the object to it and you should be good to go.
You will need to do an if statement first, if you should call the Lerp function. it looks like this:
if (Time.time - startTime < moveTime)
If you use this in FixedUpdate, replace Time.time
with Time.fixedTime
and Time.deltaTime
with Time.fixedDeltaTime
.
Thanks, but as I mentioned before, it is rotating around a cylinder. Therefore the position in y and z are constantly changing, I've tried storing the y and z in variables that change in update, but it was always a little behind causing the object to fall out of the camera's view.
Oh right, so change startPosition
with transform.position
and targetPosition
with transform.position + whateverAmountYouWantTo$$anonymous$$ove
inside the Lerp function.
Not done yet, you will need to change the time thing. I believe an if statement like this: if((Time.time - startTime) / moveTime < 1f)
would work, execute the interpolation function if that statement passes, and the time modifier would be 1f / moveTime * Time.deltaTime
or 1f / moveTime * Time.fixedDeltaTime
if you want to do this in FixedUpdate.
I edited the answer so no one will have to look in the comments.
Answer by aditya · Sep 08, 2016 at 06:34 AM
Give Vector3.MoveTowards a try, use it in Update method and you are good to go ... Good Luck
Thanks, but I can't use it in update because I need to be delayed after the key press so I need it in the coroutine. Also, I have already tried movetowards but it didn't fix my problem.