Search code examples
3dgodot

Vector3.move_toward(Vector3,delta) is not working, Godot 3d


I wanted the enemy to move towards player. So I am using Godot 3.3.4 and wrote this code, it should work. But it's not working. I wrote this code in enemy script,

self.global_transform.origin.move_toward(player.global_transform.move_toward,32) enter image description here

I feel like this should work because global_transform.origin is a vector. The "player" here has been in Onready var and the node is found.

I am not sure what I'm doing wrong.


Solution

  • Move_toward does not change the origin Vector3 directly, but returns a new Vector3 which moved towards the parameter by the delta amount.

    So to actually move your enemy you have to set the global_transform.origin to the returned value:

    global_transform.origin = global_transform.origin.move_toward(player.global_transform.origin, 32)
    

    Note: without knowing your code, but a delta of 32 might be way to fast.