My Goal: To get an SKSpriteNode to move to another SKSpriteNodes position. (To create a magnet for my sprite kit game)
Hi, I am trying to get multiple SKSpriteNodes with the same name to move towards and collide with an sprite node that i’m going to be calling a magnet i just don’t understand where or how i would go about doing this, i have tried using this code but i’m left scratching my head on what to do:
func update(_ currentTime: TimeInterval) {
let node = childNode(withName: "specialObstacle")
let magnetLocation = magnet.position
let specialObjectLocation = node?.position
let x = node?.position.x - magnetLocation.x
let y = node?.position.y - magnetLocation.y
}
Note: My magnet’s location will be changing due to the fact the user will be moving the magnet around the screen and would like it to keep going towards the magnet but i just can’t figure out how to do this.
EDIT 1
I tried using the code you had whirlwind and it seems to work for the x-axis but it seems to not work for the y axis for some reason it looks like it tries to go down but fails and just vibrates in the one spot. i have added my code below and a image
HERE is the code i'm using:
let playerLocation:CGPoint = self.convert(thePlayer.position, from: worldNode)
let location = playerLocation
let nodeSpeed:CGFloat = 3.5
worldNode.enumerateChildNodes(withName: "levelUnit"){
node, stop in
let levelUnit:LevelUnit = node as! LevelUnit //cast as an actual LevelUnit class
levelUnit.enumerateChildNodes(withName: "specialObstacle"){
node, stop in
//Aim
let dx = location.x - (node.position.x)
let dy = location.y - (node.position.y)
let angle = atan2(dy, dx)
node.zRotation = angle
//Seek
let vx = cos(angle) * nodeSpeed
let vy = sin(angle) * nodeSpeed
node.position.x += vx
node.position.y += vy
}
}