Have enemy fire when facing actor?

mhedgesmhedges Raised on VCSMember Posts: 634
edited March 2016 in Working with GS (Mac)

Hello -

I haven't quite figured out something relatively simple.

  1. I have two actors: an enemy in the center of the screen, and the player actor.
  2. I have the enemy track the player's position (using PlayerX, PlayerY, and Rotate To).
  3. When the enemy faces the player, I want to shoot a bullet. What is the proper syntax for the condition? When Enemy Rotation = ________, Spawn Actor Enemy Bullet.

Is it Enemy Rotation? Is it something different? What goes in the blank space above (#3)?

Thanks, regards.

Comments

  • Braydon_SFXBraydon_SFX Member, Sous Chef, PRO, Bowlboy Sidekick Posts: 9,271

    While this may not be exactly what you're looking for, @RThurman created this very cool way of doing a "line of sight" mechanic. Take a look and tweak to your needs.

  • owen_dennisowen_dennis Just a guy, you know. Member, PRO Posts: 236

    @Braydon_SFX said:
    While this may not be exactly what you're looking for, @RThurman created this very cool way of doing a "line of sight" mechanic. Take a look and tweak to your needs.

    That's a real clever idea!

  • mhedgesmhedges Raised on VCS Member Posts: 634

    @Braydon_SFX ,

    Thanks much! That's a cool concept.

    Honestly, I thought that there would be some behavior or expression which would do the trick. I see that the attached project is "reactionary"; that is, the enemy reacts to getting hit with the photons. I may resort to a "long stick" constrained to the enemy's rotation, if that's the trick. That way, the enemy doesn't even have to wait to get shot to react to your presence.

    Regards,
    Marcos

  • SocksSocks London, UK.Member Posts: 12,822

    @mhedges said:
    @Braydon_SFX ,

    Thanks much! That's a cool concept.

    Honestly, I thought that there would be some behavior or expression which would do the trick.

    Vector to Angle will work for this.

  • mhedgesmhedges Raised on VCS Member Posts: 634

    @Socks,

    I tried "vectorToAngle(PlayerX-self.Position.X,PlayerY-self.Position.Y)" , with self being the Enemy, but no dice. Am I missing something other than that?

  • SocksSocks London, UK.Member Posts: 12,822
    edited March 2016

    It works just fine, you've probably got it set up wrong, it's hard to say what it might be without seeing how you've got it set up, or knowing what happened when you tried it.

  • ValanValan Member, BASIC Posts: 410

    Nice problem.
    Discloser: Untried. I'll try to test this later.

    Have a hidden actor that always points to the Player.
    If the enemy rotation = the hidden actor rotation then Spawn bullets.

    Bit of a hack but it may work.

  • mhedgesmhedges Raised on VCS Member Posts: 634

    Ok, let's look at the facts:

    Project size / screen size: iPhone 6 Landscape
    Enemy player in the middle (333.5,187.5). Player can fly to any point in the screen.

    Here's the rule stating that when the enemy is facing the ship (via vectorToAngle), spawn a bullet (it also shows a red Y just for reference):

    The funny part is that the above works only when Player Y is greater than enemy Y. In other words, the enemy fires only when the player moves "over its horizon":

    Fired (look at the Y - Text Display):

    Not fired (look at the N - Text Display):

  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922

    You might need to wrap both sides of that equation in a floor as if they are off by .001 then it won't be equal. You will need to monitor the vector to angle output and the rotation to see if they match exactly the way you have it. Remeber equal means equal not kinda equal.

  • Terrellort_GamingTerrellort_Gaming Member Posts: 93

    You attach long actor on the face of enemy, which can be used like a sensor. And when the long attached actor collides with you(which mean the face is toward you), you make the enemy bombastic bamboo the character. And the character axplode.

  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922

    @Terrellort_Gaming said:
    You attach long actor on the face of enemy, which can be used like a sensor. And when the long attached actor collides with you(which mean the face is toward you), you make the enemy bombastic bamboo the character. And the character axplode.

    There are better ways than that.

  • SocksSocks London, UK.Member Posts: 12,822
    edited March 2016

    @mhedges said:
    The funny part is that the above works only when Player Y is greater than enemy Y. In other words, the enemy fires only when the player moves "over its horizon":

    vectorToAngle returns values in the range of 0° to 180° then -180° to 0°.
    Rotation returns absolute values.

    So with vectorToAngle an object orbiting an actor (counterclockwise) will be at these angles to that actor:
    0, 45, 90, 135, 180, -135, -90, -45, 0, 45, 90, 135 . . . etc

    And if the actor were to track this orbiting object the actor's own rotation would read
    0, 45, 90, 135, 180, 225, 270, 315, 360, 405, 450, 495, 540 . . . etc

    Notice how the they are in sync for the first 180 degrees, which is why your code only works when Player Y is greater than enemy Y, as they only match in the range 0-180° (basically the top half of a circle).

    So you need to normalise both their ranges. Stick %360 on the end of the things you are comparing, so they both operate within the 0-360° range. This normalises vectorToAngle to 0-360° and stops Rotation going beyond 360° (after more than one rotation).

    So:

    When self.Rotation%360 = vectorToAngle(playerX-selfX,playerY-selfY)%360

  • SocksSocks London, UK.Member Posts: 12,822

    @Lost_Oasis_Games said:
    You might need to wrap both sides of that equation in a floor as if they are off by .001 then it won't be equal. You will need to monitor the vector to angle output and the rotation to see if they match exactly the way you have it. Remeber equal means equal not kinda equal.

    Agreed, depending on how this is all set up I'd usually go for a range of values, something like plus or minus a couple of degrees. And of course a rotating actor might never 'point' at an another actor even though it passes right by it, as the vectorToAngle values on each code cycle might read . . .

    1) 14.3784011°
    2) 14.6129011°
    3) 14.8474011°
    4) 15.0819011°
    5) 15.3164011°
    6) 15.5509011°
    7) 15.7854011°

    . . . but the object to be detected might be at, for example, 15.12345678°.

  • SocksSocks London, UK.Member Posts: 12,822

    @Lost_Oasis_Games said:
    You will need to monitor the vector to angle output and the rotation to see if they match exactly the way you have it.

    Also agreed, throwing a Display Text onto each actor would have quickly shown the issue.

    Testing stuff = good :)

  • mhedgesmhedges Raised on VCS Member Posts: 634

    Gentlemen,

    Thanks much for responding and helping. I just tried it out and it works. My apologies for not doing it sooner; dealing with real world stuff on this side of the screen. Regards.

Sign In or Register to comment.