How to find Enemy is facing towards the player

0 favourites
  • 9 posts
From the Asset Store
Players follower. use it for anything which follows the player
  • Hi Team,

    I have an Enemy who is rotating in its base not moving. Player can move around it. When the Enemy face towards player enemy will shoot.

    How its possible with angle.

    How can I check Enemy's angle is towards the player

    dop2000

  • You can use angle() expression and "Is within angle" condition.

    For each enemy
    System enemy.angle is within 20 degrees from angle(enemy.x, enemy.y, player.x, player.y)
    ... Enemy spawn bullet
    
  • dop2000 Tried this

    but after 180 angletoplayer variable becomes negative

  • but after 180 angletoplayer variable becomes negative

    It doesn't matter if you use special conditions which deal with angles - like "Is between angles", "Is within angle".

    Also, you should use delta-time if you need to rotate or move characters with events. Replace "rotate 1 degrees" with "rotate 60*dt" degrees.

  • dop2000 But if I want to compare angletoplayer with selfangle will it be possible if the angletoplayer shows negative value

    suppose if I want to check whether both values are 185 or not. selfangle will show 185 but angletoplayer will show negative value when its supposed to be 185

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • suppose if I want to check whether both values are 185 or not. selfangle will show 185 but angletoplayer will show negative value when its supposed to be 185

    This is not a good idea, because the angle may not be exactly 185 degrees, it may be 184.999999999998 or something like that. So even if both angles are positive values, I still don't recommend comparing them directly.

    Use System conditions, for example "angleA Is within 0.5 degrees of 185". This will work with any angle values positive or negative, and will account for any rounding errors.

    Another option is to use anglediff expression:

    anglediff(angleA, 185)<0.5

  • dop2000 Actualy I am comparing that in a range, I said like that for easy understanding. Tried anglediff but not sure how it will work when one value becomes negative

    Got a solution here. He added 360 when it becomes negative

    https://www.construct.net/en/forum/construct-2/how-do-i-18/calculate-angle-toward-83962

  • anglediff but not sure how it will work when one value becomes negative

    Anglediff will work just fine.

    You can convert an angle to positive number using this formula: (angle+360)%360

    The result will be between 0 and 360. But I still recommend using special System conditions which are designed for working with angles.

  • Thanks will try

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)