- Home /
MoveTowards?
Been browsing the docs, and jerry-rigging a bit from the forums now, and can't seem to get what I want, so I hope someone can answer this confusion, I am sure it's super simple.
I have an object in world space eg. (0,0,0)
I have a script that detects mouse click position (world space)
Attempt I can move the object instantly, and the docs talk about moving between points over a specific amount of time, with smooth movement between them. But I am looking to not move between points on a given time, but at a given speed, no matter how long it will take to get there. So when I click somewhere, the object will start to move towards that target, at a given speed until it reaches it.
Answer by robertbu · Jan 30, 2013 at 06:07 AM
Put something like this in your Update():
transform.position = Vector3.MoveTowards(transform.position, v3TargetPosition, maxDistPerSecond * Time.deltaTime);
Answer by Deeweext · Jan 30, 2013 at 03:29 PM
The MoveObject seem to be overkilling bambi, I used Robertbu's solution and came up with this method, for future people who come across this. I have two variables, one for the target location and one for the current position. When I click somewhere, it sets the target location into that variable, and in the runetime update, it runs this:
if(myTransform != TargetLocation)
{
transform.position = Vector3.MoveTowards(transform.position, TargetLocation, 2 * Time.deltaTime);
}
THAN$$anonymous$$S A LOT $$anonymous$$AN! You saved my life!!
Answer by Eric5h5 · Jan 30, 2013 at 03:35 AM
See MoveObject, specifically when using MoveType.Speed.