Makkeonmies
Member
So in my strategy game, there's player units and then there's computer player units. There is also a third party, namely wildlife enemy units, that are also computer controlled and hostile to all (both player AND computer player units). These are supposed to attack the nearest player OR computer player units. However, if there are both of the latter present in the room, the wildlife enemy object's movement/attack code causes it to go crazy. It moves so that it doesn't know where to go, moving all over the place - whereas I only want it to chase and attack the closest units, whether that's the player's or the computer player's units doesn't matter.
It would be easy to solve with parents, but the player objects and the computer player objects can't have the same parent here. Namely because there's also a split-screen multiplayer with two players, and player 1 and player 2 units already have a common parent. So this parent can't be shared with the computer player, it only messes things up. Obviously would be easy to have the wildlife-enemies just attack a common parent with the player + computer player as children...
So how can I make this wildlife-type enemy objects just simply attack any player-type units in sight, without going crazy if there are more than one type present?
Currenly my code for the wildlife enemy is:
if playerpar > 0
{
if distance_to_object(playerpar) < sight
{
if distance_to_object(playerpar) > attackdist
{
target=instance_nearest(x,y,playerpar)
image_angle=point_direction(x,y,target.x,target.y)
mp_potential_step(target.x,target.y,spd,0)
image_speed=0.6
}
}
}
}
if playerpar> 0
{
if distance_to_object(playerpar) > attackdist
{
speed=0
}
}
if playerpar > 0
{
if distance_to_object(playerpar) < attackdist
{
if can_atk=true
{
audio_play_sound(choose(wolfattack,wolfattack2,wolfattack3),0,0)
b=instance_create(x,y,obj_wolfattack)
b.direction=image_angle
b.image_angle=image_angle
object_set_sprite(self,spr_wolf_attack)
image_speed=0.6
can_atk=false
}
}
}
Is there anything wrong there.?
It works, but if I use the same code for the computer enemy (enemypar), and both the player's and the computer enemy's units exist in the same room with the wildlife-enemy objects, then the latter goes ape for some reason and doenst know which ones to chase. I just want it to react to the nearest unit, whether it's the player or the computer's ones.
It would be easy to solve with parents, but the player objects and the computer player objects can't have the same parent here. Namely because there's also a split-screen multiplayer with two players, and player 1 and player 2 units already have a common parent. So this parent can't be shared with the computer player, it only messes things up. Obviously would be easy to have the wildlife-enemies just attack a common parent with the player + computer player as children...
So how can I make this wildlife-type enemy objects just simply attack any player-type units in sight, without going crazy if there are more than one type present?
Currenly my code for the wildlife enemy is:
if playerpar > 0
{
if distance_to_object(playerpar) < sight
{
if distance_to_object(playerpar) > attackdist
{
target=instance_nearest(x,y,playerpar)
image_angle=point_direction(x,y,target.x,target.y)
mp_potential_step(target.x,target.y,spd,0)
image_speed=0.6
}
}
}
}
if playerpar> 0
{
if distance_to_object(playerpar) > attackdist
{
speed=0
}
}
if playerpar > 0
{
if distance_to_object(playerpar) < attackdist
{
if can_atk=true
{
audio_play_sound(choose(wolfattack,wolfattack2,wolfattack3),0,0)
b=instance_create(x,y,obj_wolfattack)
b.direction=image_angle
b.image_angle=image_angle
object_set_sprite(self,spr_wolf_attack)
image_speed=0.6
can_atk=false
}
}
}
Is there anything wrong there.?
It works, but if I use the same code for the computer enemy (enemypar), and both the player's and the computer enemy's units exist in the same room with the wildlife-enemy objects, then the latter goes ape for some reason and doenst know which ones to chase. I just want it to react to the nearest unit, whether it's the player or the computer's ones.