- Home /
Is there a way to move transform.Transl by a certain distance?
private IEnumerator H(float vx, float vy, float vw)
{
vx = vx / 1000;
vy = vy / 1000;
vw = vw / 1000;
while (true)
{
transform.Translate((transform.forward * 1) * Time.deltaTime * vx, Space.World);
transform.Translate((transform.right * 1) * Time.deltaTime * vy, Space.World);
transform.Rotate((transform.up * 1000) * Time.deltaTime * vw, Space.World);
yield return 0;
}
}
Without using a function like MoveTowards in the code above I want to make it move a certain distance. I tried various methods, but it was not easy
What if you make another variable and add Time.deltaTime * vx //or vy
inside the while loop and once it reaches a certain int value stop the loop
Answer by CodeMonkeyYT · Apr 26 at 09:19 AM
If you want to move it a certain amount instantly, then just use that value in your calculation instead of Time.deltaTime
If you want it to move a certain amount over time, you can keep a simple float distanceTravelled
, when moving check how much you moved distanceTravelled += (transform.forward * Time.deltaTime).magnitude + (transform.right * Time.deltaTime).magnitude;
And do a simple if on that distanceTravelled to know when to stop.
Alternatively, if you just want smooth movement, I like to use Lerp https://unitycodemonkey.com/video.php?v=jAN2IoWdPzM
Answer by junsungkim04 · Apr 28 at 10:34 AM
Is there any way to measure the rotation y value like transform.forward? @CodeMonkeyYT