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