I\'m trying to rotate a player to the touch. Using the touches move function, I set the location to a variable. I then call a function. It works but the angle is off. Locati
According to the SpriteKit Best Practices doc
Use game logic and art assets that match SpriteKit’s coordinate and rotation conventions. This means orienting artwork to the right. If you orient the artwork in some other direction, you need to convert angles between the conventions used by the art and the conventions used by SpriteKit.
Since the default spaceship points upward (when zRotation is 0), you will need to offset your angle by 90 degrees (pi/2 radians) so that the ship faces to the right when zRotation is zero:
player?.zRotation = angle - CGFloat(M_PI_2)
Alternatively, you can rotate the spaceship to the right. To rotate the image, click on Assets.xcassets and then click on Spaceship. Right click on the Spaceship image and select the Open in External Editor menu item. This will open the image in the Preview app. In Preview, select Tools > Rotate Right and quit Preview. By rotating the artwork, your code should work without any changes.
Use an orientation constraint to rotate your object to the touch position:
// Create two global Sprite properties:
var heroSprite = SKSpriteNode(imageNamed:"Spaceship")
var invisibleControllerSprite = SKSpriteNode()
override func didMoveToView(view: SKView) {
// Create the hero sprite and place it in the middle of the screen
heroSprite.xScale = 0.15
heroSprite.yScale = 0.15
heroSprite.position = CGPointMake(self.frame.width/2, self.frame.height/2)
self.addChild(heroSprite)
// Define invisible sprite for rotating and steering behavior without trigonometry
invisibleControllerSprite.size = CGSizeMake(0, 0)
self.addChild(invisibleControllerSprite)
// Define a constraint for the orientation behavior
let rangeForOrientation = SKRange(constantValue: CGFloat(M_2_PI*7))
heroSprite.constraints = [SKConstraint.orientToNode(invisibleControllerSprite, offset: rangeForOrientation)]
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
// Determine the new position for the invisible sprite:
// The calculation is needed to ensure the positions of both sprites
// are nearly the same, but different. Otherwise the hero sprite rotates
// back to it's original orientation after reaching the location of
// the invisible sprite
var xOffset:CGFloat = 1.0
var yOffset:CGFloat = 1.0
var location = touch.locationInNode(self)
if location.x>heroSprite.position.x {
xOffset = -1.0
}
if location.y>heroSprite.position.y {
yOffset = -1.0
}
// Create an action to move the invisibleControllerSprite.
// This will cause automatic orientation changes for the hero sprite
let actionMoveInvisibleNode = SKAction.moveTo(CGPointMake(location.x - xOffset, location.y - yOffset), duration: 0.2)
invisibleControllerSprite.runAction(actionMoveInvisibleNode)
// Optional: Create an action to move the hero sprite to the touch location
let actionMove = SKAction.moveTo(location, duration: 1)
heroSprite.runAction(actionMove)
}
}
Tutorial: http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html
Video: https://youtu.be/8d8MH_gXt84
Try something like this
//set sprite to image
Person = SKSpriteNode(imageNamed: "Person")
//set size
Person.size = CGSize(width: 40, height: 7)
//set position
Person.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 120)
//rotate node 3.14 / 2 = to 90 degree angle
Person.zRotation = 3.14 / 2
Person.zPosition = 2.0