0 votes

I have a zombie enemy that chases the player as long as nothing obstructs its view i made a script that would let it continue its path to the last location where the player was "seen" by the zombie and stop.

It does it but not properly, so currently it goes the the last location but when it reaches it, it spazes out turning left and right.

The problem i found is that the position of my zombie and last_location aren't whole numbers so the elif statement doesnt work every time.

Here is the chase and late_location script, its in a separate script for enemy entities.

func chase_player():
    var to_player = player.global_position - global_position
    to_player = to_player.normalized()
    last_location = player.global_position
    global_rotation = atan2(to_player.y, to_player.x)
    movement(to_player, SPEED)

func last_location():
    var to_player = last_location - global_position
    to_player = to_player.normalized()
    global_rotation = atan2(to_player.y, to_player.x)
    movement(to_player, SPEED)

here is how i call it in my zombie scene

func _physics_process(delta):
    if can_see_player:
        chase_player()
    elif global_position != last_location:
        chase_player()

Ive been looking thought the docs about how Vector work but they don't have a lot of example on how the methods work.

I tried using floor() and ceil() but they are for single float numbers.

Is there a way to get whole numbered vectors?

in Engine by (394 points)

3 Answers

+1 vote
Best answer

to get an int vector you can do this:

var v=Vector2(1.3,2.9)
print(Vector2(floor(v.x), floor(v.y)))

print: 1,2

or

print( v.floor())

print 1,2

by (2,060 points)
selected by

Thanks it work correctly now a bit long but it gets the job done.

+3 votes

We got three different functions for that:

vector.round()
vector.floor()
vector.ceil()

Documentation for Vector2.

by (4,103 points)
0 votes

Just do

exampleVar.x = round(Vector2.x)
exampleVar.x = round(Vector2.y)
by (526 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.