- Home /
I'm getting some very strange movements from an object position
The station Gameobject teleports to the almost the center of the scene when the function is called. When it should move slowly to the right. The function is called from the startup() with a on click button listener.
I am confident it has something to do with the deltaTime. But I can't seem to solve it.
void GoToBelt(GameObject station)
{
float stationMovingSpeed= 1;
Vector3 stationUndocked = new Vector3(-16, 3.2f, 0);
float step = stationMovingSpeed * Time.deltaTime;
station.transform.position = Vector3.MoveTowards(transform.position, stationUndocked, step);
}
Answer by tormentoarmagedoom · Sep 15, 2018 at 12:22 AM
Good day.
As i supose this script is not in the station Gameobject because you need to acces it from station.transform... You miss refear to the station inside the MoveTowards function.
Replace with :
station.transform.position = Vector3.MoveTowards(station.transform.position, stationUndocked, step);
Bye!
Thanks I missed that. However now the GameObject moves 1 pixel per click of the button. I would have thought it would move fluidly after 1 click.
You'll need to review what $$anonymous$$oveTowards does. It is like a lerp. The third parameter must be able to transit between 0 and 1, the output of $$anonymous$$oveTowards will be a point between start and end points provided, but in particular can only approach the endpoint as 'step' becomes 1.0.
In your logic, there's no chance of that happening. Time.deltaTime will likely always be 1/60th of a second, meaning that the result will always be the same step. You must increment step from 0 to 1, but your code isn't doing that. $$anonymous$$ake step a member, not a local. Initialize it to zero at the beginning of this motion, and let it increment the calculation of speed * deltaTime, stopping the motion when step reaches the goal of 1.0.
I think because I was calling it from a button click listener it only fires once per click. It works fine in the update function. So now I'm knee deep in IEnumerator's and while loops.