I have a SKAction that changes the y position of a sprite. The sprite is affected by gravity towards the right, and when i set the x position in the SKAction to the sprite's position the sprite movement is slowing down while the Action is running.
How can i change this so that the x movement is not effected by the action running?
let moveDown = SKAction.moveTo(CGPoint(x: player.position.x, y: 0), duration: 0.3)
player.runAction(moveDown)
You could try using linearDamping. I'm not sure this is exactly what you're looking for, but it's a property that reduces the body’s linear velocity.
player.physicsBody!.linearDamping = 0.0
Let me know if this works for you or if this wasn't what you were looking for!
Solved it using this code:
let moveDown = SKAction.moveToY(0, duration: 0.3)
player.runAction(moveDown)
Related
I want to set a refernce point in scene, and place object at 5 meters from the reference point on X axis from example. Basically, i want to move an object without tap gesture and just using how many meters in front,right, my object should be put. Can you help me please?
When you first run an ARSession the worldOrigin is set at SCNVector3(0,0,0).
As such you can determine an initial reference point based on this.
For example placing an object 1m to the right of the XAxis, 0m on the YAxis and -1.5 away from the camera would be done like so:
let nodeToAdd = SCNNode()
let nodeGeometry = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
nodeGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
nodeToAdd.geometry = nodeGeometry
nodeToAdd.position = SCNVector3(1, 0, -1.5)
augmentedRealityView.scene.rootNode.addChildNode(nodeToAdd)
You can then move an SCNNode in many ways; with a very simple method being the use of an SCNAction which is:
A simple, reusable animation that changes attributes of any node you attach it to.
Actions which move an SCNNode are as follows:
In our case therefore, let's make use of the class func:
move(to: SCNVector3, duration: TimeInterval)
Which simply moves our node to a new position:
let moveAction = SCNAction.move(to: SCNVector3(1.5, 0, -1.5), duration: 5)
nodeToAdd.runAction(moveAction)
Hope this points you in the right direction.
i have an SKSpriteNode named MainTank however when i try to move the tank, it goes left instead of what i did, here's my code
let moveRight = SKAction.moveBy(x: MainTank.position.x + 4, y:0, duration:0.1)
MainTank.run(moveRight)
thanks
The moveBy SKAction moves the sprite by the specified amount, so adding your sprite's current position does not make sense. Not sure why it is moving left but perhaps your sprite is already at a negative position?
To move by a distance of 4 use:
let moveRight = SKAction.moveBy(x:4, y:0, duration:0.1)
Or alternatively use the moveTo SKAction.
i am following this tutorial (i think it is written in JavaScript): 3D Terrain
Im trying it in Swift with SpriteKit but have a problem with the rotation at the x-axis.
So this is what it looks like in the tutorial:
And this is where i am now:
I have created this grid with following Code:
var shapePoints = [CGPoint]()
for i in 0...zeilen+1{
for j in 0...spalten{
shapePoints.append(CGPoint(x: zellenGroesse*j, y: zellenGroesse*i))
shapePoints.append(CGPoint(x: zellenGroesse*j, y: zellenGroesse*(i+1)))
}
}
let fertigeShape = SKShapeNode(points: &shapePoints, count: shapePoints.count)
self.addChild(fertigeShape)
Now i would like to rotate it by some degrees, but i can only rotate it at the z-axis, not the x-axis.
Is there a way in SpriteKit to rotate the node at the x-axis, too?
Thanks and best regards
You can since iOS11, with what's called an SKTransformNode. When you want to 3D perspective tilt a node, add it as a child to a SKTransformNode, and then set it's xRotation or yRotation, which is a value in radians.
let t = SKTransformNode()
t.addChild(someNode)
scene.addChild(t)
t.xRotation = CGFloat.pi // tilts someNode by 180 degrees
You can also animate the change, like this.
let degrees: CGFloat = 20
let radians = CGFloat.pi / 180 * degrees
let duration = 2.0
let tilt = SKAction.customAction(withDuration: duration) { (node, elapsed) in
let percent = elapsed / CGFloat(duration)
t.xRotation = percent * radians
}
tilt.timingMode = .easeOut
t.run(tilt)
No, it isn't possible, SpriteKit is 2d. You should look at SceneKit for 3d. https://developer.apple.com/reference/scenekit
Hopefully you found a solution, but now Apple has released SKTransformNode - you can put your node(s) inside an SKTransformNode and rotate them around X and Y in addition to the standard Z
physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
let dodge = childNodeWithName(Dodge) as! SKSpriteNode
dodge.physicsBody!.applyImpulse(CGVector(dx: 100.0, dy: -100))
When the ball touches the boundaries that I set, it slows down or it just stops moving.
Allows Rotation Unchecked,
Friction to 0,
Restitution to 1,
linear Damping to 0,
Angular Damping to 0
Have you tried setting the friction to 0 on your edge based physics body? (The edge of the screen) This could be causing the problem. Even though your volume based physics body has no friction (The ball) friction will still be caused unless your edge based boundary (the edge of the screen) also has no friction set.
Have you tried applyforce instead of applyimpulse?
dodge.physicsBody!.applyForce(CGVector(dx: 100, dy: -100))
Is there a way to change the velocity/speed of a skaction and also to reset the skaction?
let wait = SKAction.waitForDuration(5.0)
let moveRight = SKAction.moveByX(300, y:0, duration: 1.0)
let sequence = SKAction.sequence([wait, moveRight])
let endlessAction = SKAction.repeatActionForever(sequence)
node.runAction(endlessAction)
This code works but the things i would like to change is how fast the SKSpriteNode moves to the right as at the moment it it quite slow and also to make the SKSpriteNode return to its orignal position rather than keep on moving to right forever?
Thank you
Since velocity = distance / time, decreasing the duration will increase the speed the the sprite will move across the screen.
Regarding your second point, by considering how SKAction.moveByX(300, y:0, duration: 1.0) moves the node to the right; SKAction.moveByX(-300, y:0, duration: 1.0) must therefore move the node to the left, back to its original position.
Hope that helps.