Please please please help me improve my glitchy chaser script

#1

so my chaser is basically a zombie who chases you whenever u get within the trigger zone

PROBLEMS FACING
-zombie sprite looks super glitch when it is moving towards the player while on the same X coordinates
(same with same Y but i know its because it is neither > or < for the script to respond)
IMPORTANT POINT : ONLY HAPPENS WHEN CHASER.X == HERO.X SAME X AXIS PLANE

cant send video, screenshot is one frame so it cant capture the slight shake of the sprite just imagine two zombies of same animation(playing not like smashing start playing) slightly different postiton flipping back and forth really quickly

-is there a way to get rid of the grip i get when i try to walk in southwest when there is a southern wall and pushing against the wall because this is causing the zombie and i to stick on the wall

INFORMATION
zombie has idle,left,right animation normally i use right for up and left for down too lazy(this time i did not add react to up and down because of < and >

all my moving scripts are edits of the walking astronaut one

SCRIPTS

THIS IS THE ZOMBIE_MOVE SCRIPT!!!

url ------ send from zombie_move script

function chase(self)
--if there is better ways to slow down the chaser plz let me know uwu
	if self.chase == true then
		msg.post("/hero#hero_enemy", "triggered", {url = self.url})
	elseif self.chase == false then
		self.enemy = nil
		msg.post("#sprite", "play_animation", { id = hash("idle") })
	end
end

function init(self)
	self.dir = vmath.vector3()
	self.enemy = nil
	self.current_anim = nil
	self.speed = 300


	--msg.post("@system:", "toggle_physics_debug")
	self.chase = false
	self.url = msg.url()
	timer.delay(0.25, true, chase)
end

function update(self, dt)

	self.anim = hash("idle")
	self.pos = go.get_position()

	if self.enemy ~= nil then

		if self.enemy.y > self.pos.y then 
			self.dir.y = self.dir.y + 1
			if self.enemy.x > self.pos.x then 
				self.dir.x = self.dir.x + 1
				self.anim = hash("right")
			elseif self.enemy.x < self.pos.x then 
				self.dir.x = self.dir.x - 1
				self.anim = hash("left")
			end
		elseif self.enemy.y < self.pos.y then 
			self.dir.y = self.dir.y - 1
			if self.enemy.x > self.pos.x then 
				self.dir.x = self.dir.x + 1
				self.anim = hash("right")
			elseif self.enemy.x < self.pos.x then 
				self.dir.x = self.dir.x - 1
				self.anim = hash("left")
			end
		end

		if vmath.length_sqr(self.dir) > 1 then
			self.dir = vmath.normalize(self.dir)
		end

		go.set_position(self.pos + self.dir * self.speed * dt)
	end

	-- animate 
	
	if self.anim ~= self.current_anim then
		msg.post("#sprite", "play_animation", { id = self.anim })
		print(self.anim)
		self.current_anim = self.anim
	end
	-- done animating
	
	self.dir = vmath.vector3()
end

function on_message(self, message_id, message, sender)
	if message_id == hash("returned_position") then
		self.enemy = message.pos

	elseif message_id == hash("trigger_response") then
		if message.enter then
			self.chase = true
		else
			self.chase = false
		end
	end
end

line``````````````````````````````````````````````

THIS IS THE HERO_ENEMY SCRIPT!!!!!!!!
url ------ /hero#hero_enemy

function on_message(self, message_id, message, sender)
	if message_id == hash("triggered") then
		local reciever_url = message.url
		self.pos = go.get_position()
		msg.post(reciever_url, "returned_position", { pos = self.pos })
	end
end

**PLEASE HEPPPP wOwOwOw **

BTW the shortcut icon for the forum has a white box around it and it is not looking good and the dashboard shortcut icon is kinda pixalated

0 Likes

#2

So you have two problems? One is that you get stuck in the wall and the second is that the zombie has some jitter when moving?

Have you checked some of the examples showing how you separate collision objects?

0 Likes

#3

nani what examples

0 Likes

#4

Here’s the manual:

And a tutorial:

1 Like

#5

Ive did another hour of debugging last night and this is what ive came up with

1.if i do a

self.pos = go.get_position()
print(self.pos)
in the update function

it will give me something like this:

DEBUG:SCRIPT: vmath.vector3(1510.1380615234, 213.27543640137, 1)
DEBUG:SCRIPT: vmath.vector3(1512.8471679688, 209.73989868164, 1)
DEBUG:SCRIPT: vmath.vector3(1513.6735839844, 213.27543640137, 1)
DEBUG:SCRIPT: vmath.vector3(1516.3826904297, 209.73989868164, 1)
DEBUG:SCRIPT: vmath.vector3(1517.2091064453, 213.27543640137, 1)
DEBUG:SCRIPT: vmath.vector3(1519.9182128906, 209.73989868164, 1)
DEBUG:SCRIPT: vmath.vector3(1520.7446289063, 213.27543640137, 1)
DEBUG:SCRIPT: vmath.vector3(1523.4537353516, 209.73989868164, 1)
DEBUG:SCRIPT: vmath.vector3(1524.2801513672, 213.27543640137, 1)
DEBUG:SCRIPT: vmath.vector3(1526.9892578125, 209.73989868164, 1)
DEBUG:SCRIPT: vmath.vector3(1527.8156738281, 213.27543640137, 1)

even through the zombie is suppose to move along the x axis because the y axis is the same
the zombie is slight going up and down and up and down and the y axis is bouncing from these two numbers.

i tried everything including ignoring the y axis movement and

if self.enemy == self.pos then
end
still it says that their y axis isnt the same

i think its the y axis neither being higher nor lower that is causing the zombie to do both resutng in jittering

now if i do a

self.pos = go.get_position()
		print(self.pos)

	if self.enemy.x > self.pos.x then 
		self.dir.x = self.dir.x + 1
		self.anim = hash("right")
		if self.enemy.y > self.pos.y then 
			print("move_up")
			self.dir.y = self.dir.y + 1
		elseif self.enemy.y < self.pos.y then 
			print("move_down")
			self.dir.y = self.dir.y - 1
		else
		end
	elseif self.enemy.x < self.pos.x then 
		self.dir.x = self.dir.x - 1
		self.anim = hash("left")
		if self.enemy.y > self.pos.y then 
			print("move_up")
			self.dir.y = self.dir.y + 1
		elseif self.enemy.y < self.pos.y then 
			print("move_down")
			self.dir.y = self.dir.y - 1
		else
		end
	end

i will get this

DEBUG:SCRIPT: vmath.vector3(1866.3994140625, 136.97384643555, 1)
DEBUG:SCRIPT: move_down
DEBUG:SCRIPT: vmath.vector3(1868.8266601563, 133.43830871582, 1)
DEBUG:SCRIPT: move_up
DEBUG:SCRIPT: vmath.vector3(1869.9349365234, 136.97384643555, 1)
DEBUG:SCRIPT: move_down
DEBUG:SCRIPT: vmath.vector3(1872.3621826172, 133.43830871582, 1)
DEBUG:SCRIPT: move_up
DEBUG:SCRIPT: vmath.vector3(1873.4704589844, 136.97384643555, 1)
DEBUG:SCRIPT: move_down
DEBUG:SCRIPT: vmath.vector3(1875.8977050781, 133.43830871582, 1)
DEBUG:SCRIPT: move_up
DEBUG:SCRIPT: vmath.vector3(1877.0059814453, 136.97384643555, 1)
DEBUG:SCRIPT: move_down
DEBUG:SCRIPT: vmath.vector3(1879.4332275391, 133.43830871582, 1)
DEBUG:SCRIPT: move_up
DEBUG:SCRIPT: vmath.vector3(1880.5415039063, 136.97384643555, 1)
DEBUG:SCRIPT: move_down
DEBUG:SCRIPT: vmath.vector3(1882.96875, 133.43830871582, 1)
DEBUG:SCRIPT: move_up

but if i remove the reaction for up down movement, it turns out it is jittering both left right up and down now i am very confused and cant fix it plz help

SCRIPT

function chase(self)
	if self.chase == true then
		msg.post("/hero#hero_enemy", "triggered", {url = self.url})
	elseif self.chase == false then
		self.enemy = nil
		msg.post("#sprite", "play_animation", { id = hash("idle") })
	end
end

function init(self)
	self.dir = vmath.vector3()
	self.enemy = nil
	self.current_anim = nil
	self.speed = 300


	--msg.post("@system:", "toggle_physics_debug")
	self.chase = false
	self.url = msg.url()
	timer.delay(0.25, true, chase)
	self.play_animation = true
end

function update(self, dt)

	self.anim = hash("idle")

	if self.enemy ~= nil then

		--[[print("enemy")
		print(self.enemy)
		print("self")
		print(self.pos)]]

		self.pos = go.get_position()
		print(self.pos)

		if self.enemy.x > self.pos.x then 
			self.dir.x = self.dir.x + 1
			self.anim = hash("right")
			if self.enemy.y > self.pos.y then 
				print("move_up")
				self.dir.y = self.dir.y + 1
			elseif self.enemy.y < self.pos.y then 
				print("move_down")
				self.dir.y = self.dir.y - 1
			else
			end
		elseif self.enemy.x < self.pos.x then 
			self.dir.x = self.dir.x - 1
			self.anim = hash("left")
			if self.enemy.y > self.pos.y then 
				print("move_up")
				self.dir.y = self.dir.y + 1
			elseif self.enemy.y < self.pos.y then 
				print("move_down")
				self.dir.y = self.dir.y - 1
			else
			end
		end
		
		if vmath.length_sqr(self.dir) > 1 then
			self.dir = vmath.normalize(self.dir)
		end

		go.set_position(self.pos + self.dir * self.speed * dt)
	end

	-- animate 
	if self.play_animation == true then
		if self.anim ~= self.current_anim then
			msg.post("#sprite", "play_animation", { id = self.anim })
			self.current_anim = self.anim
		end
	elseif self.play_animation == false then
		msg.post("#sprite", "play_animation", { id = hash("attack") })
	end
	-- done animating
	
	self.dir = vmath.vector3()
end

function on_message(self, message_id, message, sender)
	if message_id == hash("returned_position") then
		self.enemy = message.pos
	elseif message_id == hash("attack") then
		self.play_animation = false
	elseif message_id == hash("trigger_response") then
		if message.enter then
			self.chase = true
		else
			self.chase = false
		end
	elseif message_id == hash("animation_done") then
		-- start the walk animation
		self.play_animation = true
	end
end
0 Likes

#6

Share the entire project if possible.

0 Likes

#7

how to do that?

0 Likes

#8

Simple. Zip your project folder, and upload it on the forum. :slight_smile:

1 Like

#9

living mosters.zip (270.9 KB)

huzzah a zipped file

how magnificent

0 Likes

#10

You had this:

	if self.enemy.x > self.pos.x then 
		self.dir.x = self.dir.x + 1
		self.anim = hash("right")
		if self.enemy.y > self.pos.y then 
			print("move_up")
			self.dir.y = self.dir.y + 1
		elseif self.enemy.y < self.pos.y then 
			print("move_down")
			self.dir.y = self.dir.y - 1
		else
		end
	elseif self.enemy.x < self.pos.x then 
		self.dir.x = self.dir.x - 1
		self.anim = hash("left")
		if self.enemy.y > self.pos.y then 
			print("move_up")
			self.dir.y = self.dir.y + 1
		elseif self.enemy.y < self.pos.y then 
			print("move_down")
			self.dir.y = self.dir.y - 1
		else
		end
	end
	
	if vmath.length_sqr(self.dir) > 1 then
		self.dir = vmath.normalize(self.dir)
	end
	
	go.set_position(self.pos + self.dir * self.speed * dt)

But this is actually enough:

	self.dir = self.enemy - self.pos
	if self.enemy.x > self.pos.x then 
		self.anim = hash("right")
	elseif self.enemy.x < self.pos.x then 
		self.anim = hash("left")
	end

PS I saw that you spread your enemy logic into multiple scripts: one for moving, one for handling collisions, and another for attacking. From a performance perspective it is better to have a single script and put all of the logic in there OR to put the logic in Lua modules and use those from the single script. But, depending on the scope and target platform and the number of monsters etc it might work well enough to keep it as it is.

	self.dir = vmath.normalize(self.dir)
	
	go.set_position(self.pos + self.dir * self.speed * dt)
1 Like

#11

omg owo thank u so muchh Uwu

0 Likes

#12

Separate note: I saw that you previously tried:

if self.enemy == self.pos then

But that only works well if you are using whole integers (0,1,2,3 etc)
If you are using real numbers (i.e. decimals 10.7123, 19.4532 etc) you will most often run into trouble when comparing numbers like that.
Instead, in that case, try to detect if the two numbers are close enough:

if vmath.length_sqr(pos_a - pos_b) < 0.01 then …

2 Likes