Round Vector2 to whole numbers.

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

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?

:bust_in_silhouette: Reply From: estebanmolca

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

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

Newby | 2020-01-31 00:32

:bust_in_silhouette: Reply From: Dlean Jeans

We got three different functions for that:

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

Documentation for Vector2.

:bust_in_silhouette: Reply From: Merlin1846

Just do

exampleVar.x = round(Vector2.x)
exampleVar.x = round(Vector2.y)