This should be simple, I know, but for some reason, I can’t seem to wrap my mind around it…
I want to move an object from one position to another in X number of seconds. I can’t figure out how to make it work correctly.
I would like to use Vector3.MoveTowards(), but will take any solution.
I tried taking a mathematic approach, but I haven’t touched much math since high school, so be nice.
The way I’ve always used MoveTowards() is with the last argument essentially equating to the speed at which the transition should happen. I’m not sure if this is correct or not, as the last argument listed as “Max Distance Delta” doesn’t seem all that intuitive.
Anyway… I figured I needed to calculate the speed for my final argument, so I deduced:
distance = d
speed = s
time = t
d = s * t
so
s = d / t
I calculate the distance that needs to be traveled by subtracting my two distances (although they’re vector3s only the y value is different), let’s call this value posDiff.
Then, I attempt my transition as follows:
transform.position = Vector3.MoveTowards(transform.position, newPosition, Time.DeltaTime * (posDiff / transitionDuration))
I use an intermediate value for the transform, then assign that value to my gameobject’s transform in my actual code, but for demonstration purposes, I think the above suffices.
Any help would be awesome.