How to move the enemy towards a moving player?

I am creating a simple game with a set of sprites that positions the player on the left side of the screen, while the enemies come up on the right. Since the player can move up and down, I want the enemies to โ€œsmartlyโ€ adjust their path to the player.

I tried to remove and re-add the SKAction sequence whenever the player moves, but the code below makes the enemies not show at all, probably because he simply adds and removes every action every time the frame is updated, so they will never have the opportunity to move .

Hoping to get a little feedback on the best practice for creating smart enemies that will move to the playerโ€™s position at any time.

Here is my code:

func moveEnemy(enemy: Enemy) { let moveEnemyAction = SKAction.moveTo(CGPoint(x:self.player.position.x, y:self.player.position.y), duration: 1.0) moveEnemyAction.speed = 0.2 let removeEnemyAction = SKAction.removeFromParent() enemy.runAction(SKAction.sequence([moveEnemyAction,removeEnemyAction]), withKey: "moveEnemyAction") } func updateEnemyPath() { for enemy in self.enemies { if let action = enemy.actionForKey("moveEnemyAction") { enemy.removeAllActions() self.moveEnemy(enemy) } } } override func update(currentTime: NSTimeInterval) { self. updateEnemyPath() } 
+3
source share
1 answer

In each update, it is necessary to update the position and zRotation enemy.

Seeker and purpose

Ok, so add some nodes to the scene. We need a seeker and a goal. The seeker will be a rocket, and the target will be a touch. I said that you should do this inside the update method: but I will use the touchhesMoved method to make a better example. Here's how you should set up the scene:

 import SpriteKit class GameScene: SKScene, SKPhysicsContactDelegate { let missile = SKSpriteNode(imageNamed: "seeking_missile") let missileSpeed:CGFloat = 3.0 override func didMoveToView(view: SKView) { missile.position = CGPoint(x: frame.midX, y: frame.midY) addChild(missile) } } 

Aiming

To realize the goal, you need to calculate how much you need to rotate the sprite based on your goal. In this example, I will use a rocket and point it to the place of contact. To accomplish this, you must use the atan2 function, like this (inside the touchesMoved: method:

 if let touch = touches.first { let location = touch.locationInNode(self) //Aim let dx = location.x - missile.position.x let dy = location.y - missile.position.y let angle = atan2(dy, dx) missile.zRotation = angle } 

Note that atan2 takes parameters in y, x order, not x, y.

So, right now we have a corner in which the rocket should go. Now let's update its position based on this angle (add this method inside touchesMoved: right under the aiming part):

 //Seek let vx = cos(angle) * missileSpeed let vy = sin(angle) * missileSpeed missile.position.x += vx missile.position.y += vy 

And that would be so. Here is the result:

rocket search

Notice that in the Sprite-Kit, an angle of 0 radians indicates the positive x axis. And the positive angle is counterclockwise:

polar coordinates

More details here .

This means that you must orient your walkie-talkie to the right. missile nose points to the right not up enter image description here . You can also use an image with an upward orientation, but you will need to do something like this:

 missile.zRotation = angle - CGFloat(M_PI_2) 
+9
source

Source: https://habr.com/ru/post/1013255/


All Articles