- Home /
Moving an object towards hit.point
Hi!
I'm currently making a Dungeon Crawler-type game and I'm having a little bit of trouble with the movement of the character.
The way that it works right now is that I have an waypoint object which is moved around the game world using Raycast.
Moving the waypoint around like this works, moving the character to the waypoint has presented more of a challenge to me though. Looking up earlier answers I've found this script:
var target : Transform; var speed = 5.0;
function Update () {
transform.position = Vector3.Lerp (
transform.position, target.position,
Time.deltaTime* speed);
}
While this script works in a way (it does move the character to the waypoint) it moves the character as if it was connected to the waypoint with a spring and as such moves faster the farther away you put the waypoint.
What I would like is to move the character at a constant speed toward the waypoint. Anyone got any idea how to achieve this?
Answer by Wolfram · Aug 16, 2010 at 10:57 PM
float distance=Vector3.Distance(transform.position, target.position);
if(distance>0){
transform.position = Vector3.Lerp (
transform.position, target.position,
Time.deltaTime* speed/distance);
}
Thank you so much, I've been trying to figure this out for hours now. Found this answer by complete accident.
Your answer

Follow this Question
Related Questions
Move object A towards object B 2 Answers
Enemy Nudging Towards Player 1 Answer
Moving an Object towards another Moving Object 2 Answers
How to stop an object in every waypoint? 1 Answer
Object move without an input 1 Answer