(Swift SpriteKit) Rotate sprite in the direction of touch

前端 未结 4 1642
没有蜡笔的小新
没有蜡笔的小新 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

    If you want to rotate only you shouldn't call the moveTo action. Calculate the angle between touch location and location of the cannon and call action rotateToAngel

    here is the code

            let angle = atan2(dy, dx) - CGFloat(M_PI_2)
            let direction = SKAction.rotateToAngle(angle, duration: 0.4, shortestUnitArc: true)
            cannon.runAction(direction)
    

提交回复
热议问题