How do u make the enemy shoot to the player's position?

TakechuTakechu Member Posts: 9
edited November -1 in Working with GS (Mac)
I need help on making the enemy ships shoot at the player ship. As in, if im at coordinates 200x 800y; the enemy will shoot there, but the laser wont follow me like a missile. I've tried many things but.. I can't figure it out

Comments

  • TakechuTakechu Member Posts: 9
    I've done what you said, works almost fine.
    the bullets keep returning to me no matter what.
    I also tried putting a destroy after 3 seconds rule, yet.. it curves right after it passes ship and crashes with back end of ship. =\
  • TakechuTakechu Member Posts: 9
    Yes. If im not mistaken, thats where its scene>layers>(default name would be Background)
  • TakechuTakechu Member Posts: 9
    If it would clear things up, its not to follow me like a missile, i saw in my original post, its confusing, what i really want is that the bullet will follow a straight path to the location of the player 1 ship when it was fired, so when u move away it will go straight and not follow

    sorry for the poor expression in original post.
  • JohnGreenArtJohnGreenArt Member Posts: 19
    To have the enemy fire at the player, but not have the bullet continue to follow the player, you need to figure out the angle between the enemy and the player at the moment the enemy fires. There might be an easier way to do this, but I've used the vectorToAngle behavior to achieve this.

    Like TSB stated, create global attributes named something like "playersX" and "playersy" and on the player ship actor constrain them to the player's self.position x and y attributes.

    In the enemy actor add a "spawn actor" behavior (you'll probably want to put this in a timer or other sort of condition to determine how frequent the enemy fires.)

    Spawn your enemy's bullet and for the "direction" add this expression from the drop down selections:
    vectorToAngle( self.position.X - game.playersX , self.position.Y - game.playersY )-180

    In the bullet actor add a move or accelerate speed, plus any other behaviors you want it to have (collide, destroy, etc.)

    That should do it!
  • TakechuTakechu Member Posts: 9
    Thank you very much JohnGreenArt and thanks tshirtbooth as well, this has opened up many doors on how i can improve on my game =).
  • millerbrother1millerbrother1 Member Posts: 108
    why is this not working for me? :-( My enemy is positioned near the top of the screen and my player is near the bottom of the screen. However, using the exact logic above the enemy shoots the spawned actor up or to the left and off the screen.

    I must be missing something.
  • ORBZORBZ Member Posts: 1,304
    There are two ways to do this:

    option A)
    Have the missile spawn facing the player using vectorToAngle(playerX - spawnX, playerY - spawnY), then just accelerate 0 degrees relative to the actor (the actor in this case is the missile)

    option B)
    when the missile first spawns, capture the player's x/y position to some internal self variables using CHANGE attribute. Then accelerate towards that saved position, not the player's position.
  • millerbrother1millerbrother1 Member Posts: 108
    Something else must be going on with my scene. The "acorns" (my missiles) are flying up over the head of the enemy and off the scene. Here are my settings:

    My main character has two constraints:
    - Constrain game.PlayerX to Self.PositionX
    - Constrain game.PlayerY to Self.PositionY

    The enemy (pacing via the pacing tutorial on wiki) spawns an actor (we'll call it an Acorn) every 4 seconds. The spawn settings look like this (see screen shot below):

    image

    This is what the expression looks like when expanded

    image

    The acorn actor (my missile) has the following accelerate logic.

    Accelerate > Direction = 0, Relative to actor (self), Acceleration = 100

    Any reason why the acorns are flying up into the sky instead of down toward my Player?
  • millerbrother1millerbrother1 Member Posts: 108
    So, for whatever reason, when I removed the "-180" from the end of the vectorToAngle, and I set the Accelerate direction to 45 degrees it works like a charm.

    Go figure.
  • millerbrother1millerbrother1 Member Posts: 108
    Ok - so I was wrong. It still doesn't work. The closer I am to directly under the "acorn chucker" the more accurate the toss. When my player moves to the opposite side of the screen, the acorn sails well over my head.

    I'm definitely doing something wrong. :-(
  • gazjmgazjm Member Posts: 578
    You have got game.playerx-self.positiony. Needs to be game.playerx-self.positionx. Also when I do it I would put them in the other order so it's self.positionx-game.playerx,self.positiony-game.playery.

    Cheers
  • millerbrother1millerbrother1 Member Posts: 108
    Seriously? I must be an idiot! I can't believe I posted this and didn't see that. What, am I like 2 years old or something?

    This is me blushing in the presence of my forum friends.

    =^_^=
  • ORBZORBZ Member Posts: 1,304
    also, i'd get rid of the -180. that's totally not needed.

    In the expression vectorToAngle(x1-x2, y1-y2)

    The direction you want the vector to point is your x1, y1. So if you want the vector to point at your player make x1 and y1 your player's x and y.

    The -180 isn't needed at all.
  • millerbrother1millerbrother1 Member Posts: 108
    I tried it without the -180 and it shot skyward again. With it in, it's aiming right for my player's heart.

    Not sure why.
Sign In or Register to comment.