I am making a top view medieval game using HTML, JS and canvas.
The problem is that my player is able to hit to others when not looking at them. I made a condition in the third line in the below code to address this problem, but that is not working because the player's look angle limits are max 180
and min -180
. The limits between the player's position and victim's position are max 90
min -270
. These limits ruin my condition.
I can accept any ideas or readymade condition or code.
if(Math.sqrt(Math.pow((this.X-Victim.X),2),Math.pow((this.Y-Victim.Y),2)) <= 100){//<-here calculates distance between victim and player
var angle = Math.atan2(this.X - Victim.X , this.Y - Victim.Y) * (180 / Math.PI);//<-here calculates angle between victim and player
if((angle < this.LookAngle + 45)&&(angle > this.LookAngle - 45)){//<- i need help on here
console.log(angle,this.LookAngle);
//socket.emit('HitTo',{UserName:Victim.UserName,hitval:20});//<- actually you dont need to know this
}
}