how to move character to enemy position constant pace?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bgegg

i moved enemy towards character.
but it is not constant pace.
the speed slow down toward the end.

i am moving enemy with move_and_slide of kinematicbody class.
can i move that with constant pace?
please help about this.
https://i.imgur.com/bosmXAq.mp4

extends KinematicBody
onready var target = get_parent().get_node("target");#target character node
var velocity = Vector3();

func _physics_process(delta):
	velocity = (target.transform.origin - self.transform.origin);
	move_and_slide(velocity);
	move_and_collide(velocity);
1 Like
:bust_in_silhouette: Reply From: Ninfur

Velocity is the distance an object is moved every frame. Right now you are setting the velocity, or “speed”, to be the difference between the character position and the enemy position. This difference will get smaller the closer the enemy gets to the player, meaning the velocity will also get smaller the closer the enemy gets to the player.

To fix this, what you want to do is calculate a direction vector. A direction vector is the same as the difference you already calculated, but always has a length of 1. This is achieved by using the normalized function, like so:

var direction = (target.transform.origin - self.transform.origin).normalized();

You can verify that the direction vector has a length of 1 by calling

print(direction.length())

Since this direction vector always has a length of 1, the velocity will not slow down the closer the enemy gets to its target. However, the speed will also be extremely slow. To counter this we multiply the direction vector by the speed we want.

You should now have something like this:

var speed = 100;
var direction = (target.transform.origin - self.transform.origin).normalized();
velocity = direction * speed;

Finally, I would recommend multiplying the velocity by the delta value that is included in the _physics_process function. This makes is so that the velocity remains stable, regardless of the FPS, and you also gain the benefit that the speed now indicates how many units the objects moves every second, rather than meaning something arbitrary.

In the end you should have something like this:

extends KinematicBody
onready var target = get_parent().get_node("target");#target character node
var velocity = Vector3();
var speed = 100;

func _physics_process(delta):
    var direction = (target.transform.origin - self.transform.origin).normalized();
    velocity = direction * speed * delta;
    move_and_slide(velocity);
    move_and_collide(velocity);

thanks for comment.sorry my reply was very late.
it took a long time to understand about vector.

your code work well about speed.
but when enemy reached to target,enemy get vibration.

Is there a fix way?

bgegg | 2022-04-07 12:35

I would handle this by only moving the enemy closer if it’s more than X units aways from the player.

You have already calculated the difference in position between the enemy and the player with this piece of code:

target.transform.origin - self.transform.origin

To find the length between the enemy and the player, all you have to do is use the length() function on the difference vector.

(target.transform.origin - self.transform.origin).length()

Finally, use an if-statement to check if the length between the player and the enemy is greater than som value, then move the enemy. Otherwise don’t move the enemy.

I would do something like this:

func _physics_process(delta):
    var diff = target.transform.origin - self.transform.origin
    var direction = diff.normalized()
    var distance = diff.length()
    if distance > 1:
            velocity = direction * speed * delta
    else:
            velocity = Vector3.ZERO # Same as Vector3(0, 0, 0)
    move_and_slide(velocity)
    move_and_collide(velocity)

Visual that might help

Ninfur | 2022-04-07 15:38

thanks for comment.
i finally understand in now.

i will deepenmy understanding.

bgegg | 2022-04-08 12:18