Movement problems

Godot Version

4.2.2

Question

Hello friends! I have a problem, I want my NPC to move towards some coordinates, but it doesn’t, so I make it draw a line from its current position to the target position and the line is drawn correctly but the movement is wrong. The objective is that the NPC must generate a path, then it must travel that path and start the process again, can you help me with that?


This is the code fragment, which you should move to the npc


This is the result; the red line is the npc’s route and the green line is where it should move


And this is the console output

You are setting your new point as velocity. What you want to do instead is:
velocity = (new_pos - global_position).normalized() * delta * speed
To get the direction towards the new point. Also if your npc is a CharacterBody2D you should not use delta in the calculation, since CharacterBody2D adjusts the velocity based on delta on its own.

Your code for detecting if the new point is reached should also be adapted to check for the distance between your two points being below a certain threshold.

Edit: Added normalization to the direction as well, so that it has a uniform length of 1. Otherwise the movement speed will not be consistent.

3 Likes

Problem solved, thank you very much :grin:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.