(Swift SpriteKit) Rotate sprite in the direction of touch

前端 未结 4 1457
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 08:06

On the screenshot provided, the red arrow and cross are just for demonstration purposes and are not in the game. I would like the sprite of the spaceship to face the direction o

4条回答
  •  走了就别回头了
    2021-02-10 09:07

    I found that with the answer submitted by James that this caused it to rotate in the wrong way, fine if you have something like a canonball but as it was used on my game which had a mage and a fireball with a trail it caused an issue, this can be easily fixed with

     let angle = atan2(touchlocation.x - cannon.position.x , touchlocation.y - 
     cannon.position.y)
     cannon.zRotation = -(angle - CGFloat(Double.pi/2))
    

    use Double.pi / 2 and M_PI_2 is depriciated now

提交回复
热议问题