Have enemy fire when facing actor?
mhedges
Raised on VCSMember Posts: 634
Hello -
I haven't quite figured out something relatively simple.
- I have two actors: an enemy in the center of the screen, and the player actor.
- I have the enemy track the player's position (using PlayerX, PlayerY, and Rotate To).
- 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
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.
My GameSalad Academy Courses! ◦ Check out my quality templates! ◦ Add me on Skype: braydon_sfx
That's a real clever idea!
Vote for Nearest Neighbor Scaling option in gamesalad! Let's make our games look truly stunning!
@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
My Blog / App Store / Google Play
Vector to Angle will work for this.
@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?
My Blog / App Store / Google Play
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.
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.
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):
My Blog / App Store / Google Play
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.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
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.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
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
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°.
Also agreed, throwing a Display Text onto each actor would have quickly shown the issue.
Testing stuff = good
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.
My Blog / App Store / Google Play