Collisions not registering (SOLVED)

Hi again, I have enemy and player objects that im trying to get collisions to register between, but nothing is seeming to register. I’ve tried changing the collision object types for both but i cant seem to get any kind of response. Any help would be appreciated!
Player script:

go.property("dir", vmath.vector3())
local reg_speed = 75
local sprint_speed = 100
local health = 100
local damage_flag = false

local function game_over(self)
	print("GAME OVER")
end

local function take_damage(self,damage)
	if self.health <= 0 then
		game_over(self)
		print(self.health)
	else
		self.health = self.health - damage
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")
	self.dir = vmath.vector3()
	self.current_anim = nil
	self.speed = reg_speed
	self.attack = false
	self.health = health
	self.damage_flag = damage_flag
end

function update(self, dt)
	print(self.health)
	if vmath.length_sqr(self.dir) > 1 then
		self.dir = vmath.normalize(self.dir)
	end
	local p = go.get_position()
	go.set_position(p + self.dir * self.speed * dt)

	local anim = hash("idle")

	if self.attack == true then
		anim = hash("attack-f")
		self.attack = false
	end

	if self.speed == reg_speed then
		if self.dir.x > 0 then
			anim = hash("player-right")
		elseif self.dir.x < 0 then
			anim = hash("player-left")
		elseif self.dir.y > 0 then
			anim = hash("player-back")
		elseif self.dir.y < 0 then
			anim = hash("player-front")
		end
	elseif self.speed == sprint_speed then
		if self.dir.x > 0 then
			anim = hash("player-right")
		elseif self.dir.x < 0 then
			anim = hash("player-left")
		elseif self.dir.y > 0 then
			anim = hash("sprint-b")
		elseif self.dir.y < 0 then
			anim = hash("sprint-f")
		end
	end

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

function on_input(self, action_id, action)
	if action_id == hash("sprint") then
		if action.pressed then
			self.speed = sprint_speed
		elseif action.released then
			self.speed = reg_speed
		end
	elseif action_id == hash("attack") then
		self.attack = true
		take_damage(self, 5)
	elseif action_id == hash("down") then
		self.dir.y = -1
	elseif action_id == hash("up") then
		self.dir.y = 1
	elseif action_id == hash("left") then
		self.dir.x = -1
	elseif action_id == hash("right") then
		self.dir.x = 1
	end
end


function on_message(self, message_id, message, sender)
	if message_id == hash("collision_response") then
		print("Collision")
	end
end

enemy script:

go.property("dir", vmath.vector3())
local reg_speed = 50
local radius = 100
local health = 50

local function take_damage(self,damage)
	self.health = self.health - damage
end

function init(self)
	self.dir = vmath.vector3()
	self.speed = reg_speed
	self.health = health
	self.arrived = true
	self.target = vmath.vector3()
end

function update(self, dt)
	local p = go.get_position()
	local player_pos = go.get_position("Player")
	local direction = player_pos - p

	if vmath.length(player_pos - p) <= radius then
		if vmath.length_sqr(direction) > 0 then
			direction = vmath.normalize(direction)
		end
		go.set_position(p + direction * self.speed * dt)
	else
		if self.arrived == true then
			local x = math.random(50,450)
			local y = math.random(50,450)
			self.target = vmath.vector3(x,y,1)
			self.arrived = false
		end
		local distance = self.target - p
		if vmath.length_sqr(distance) > 0 then
			distance = vmath.normalize(distance)
		end
		if self.arrived == false then 
			go.set_position(p + distance * self.speed * dt)
			if vmath.length_sqr(self.target - p) < 1 then
				self.arrived = true
			end 
		end
	end
end

Have you appropriately set Group and Mask properties in enemies and player? Just the first guess for a non registering collision.

Yea ive set the player group to player and the mask to enemy and vice versa for the enemy

Enable physics debugging and verify that your collision objects are where you expect them to be.

1 Like

I’ve tried redoing the collision objects and i’ve turned on the physics debugging, but i’m finding that when i run the game the boxes arent showing up at all. Are there any other settings worth checking?

That’s strange. What if you try another project? Perhaps this one:

sorry for taking so long to reply, this is all i see when physics debug is turned on

Ok, that looks good. And I suppose the collisions are working? Did you compare the Pixel Line Sample project with your own?

yea, ive set up the collision object + hitboxes with all the same settings but nothing shows up when i run the code. I added this function to the player script to see if anything is actually registering but it seems like nothing i do actually gets the hitboxes to show up

function on_message(self, message_id, message, sender)
	if message_id == hash("collision_response") then
		print("Collision")
	end

Nevermind, problem fixed! For some reason the collision object wasnt in the main collection, the boxes are showing up now. Thanks for the help!!

2 Likes