Lookat Object

Ive been making a topdown for shooter the past few weeks and im on the part where i have to make the enemies script. Ive made the roaming movement for them and but im having a problem with making the enemy look towards the charactor so that it can shoot (the shooting works). Ive looked at the docs and for how javascript does it and it works but i dont know how to convert it into WADE so if anyone knows how to please help.

This is the rotation thing i found for javascript if that helps.


Thanks in advance.

All 2 Comments


To rotate an enemy so it faces the player, you can do something like this in the enemy's onUpdate function:

var d = wade.vec2.sub(_.player.getPosition(), this.getPosition());
var angle = Math.atan2(d.y, d.x);

Depending on where the "face", i.e. the forward part of your enemey sprite is, you may need to do

this.setRotation(angle + offset);

where offset is an angle in radians (e.g. offset = Math.PI / 2 if the forward part of the character is on the right rather than the top).


Works perfectly thank you, though it might be nice to add a vec2.lookat().

Post a reply
Add Attachment
Submit Reply
Login to Reply