I'm not a physics expert. However, I want to move UILable which has a dynamic height (depends on content) just like teleprompter. But when I start behaviour with magnitude 10, it starts moving but suddenly its slow down and I want continuous move up at the same speed.
Below is my code :
push = UIPushBehavior(items: [lblText], mode: .instantaneous)
push.setAngle(-.pi/2, magnitude: 10)
animator.addBehavior(push)
lblText size is 375*1500
Josh is right, try adding friction and resistance.
To add friction, you will have to create UIDynamicItemBehavior
let behavior = UIDynamicItemBehavior.init(items: [lblText])
Create this with the items that you need to perform your animations on.
Then you can add friction and resistance to it
behavior.friction = 0
behavior.resistance = 0
And finally add the behavior to the animator
animator.addBehavior(behavior)
Let me know if this works, happy to help.
Related
I want to create game that speedup every seconds. If I want to achieve something like this in Unity, I will just scale Time.timeScale param. Is there something like this in SpriteKit? Or should I just set diffrent speed and spawn params?
You change the speed variable on your scene and physics world
E.G. To make your game twice as fast
let scaledTime : CGFloat = 2
scene.speed = scaledTime
scene.physicsWorld.speed = scaledTime
My game has a Ferris Wheel with 4 seats. Each seat has a platform that the hero can rest on. When the seat is on the upward trajectory the hero calmly stays on the platform.
However, when the seat is on the downward trajectory the Hero moves up/down a bit.
I've tried a few obvious things:
1. Setting the restitution to 0 has no effect.
2. Setting linearDamping to 1 has no effect.
3. Making the mass of the platform and hero the same has no effect.
4. Adjusting friction has no effect.
Here is the platform physics body:
supportNode?.physicsBody?.categoryBitMask = PhysicsCategory.ferrisPlatform.rawValue
supportNode?.physicsBody?.mass = 1000
supportNode?.physicsBody?.restitution = 0.0
supportNode?.physicsBody?.friction = 0.0
supportNode?.physicsBody?.linearDamping = 1.0
Here is the hero body:
self.physicsBody?.linearDamping = 1.0
self.physicsBody?.mass = 30
self.physicsBody?.restitution = 0
self.physicsBody?.friction = 0
Thanks for any tips. Its definitely bizarre that the hero is fine on the way up on the ferris wheel ride but only shows quirky up/down movement on the way down.
It seems like a problem of mass to me. The platform's mass has nothing to do with it because I read that it's pinned to the wheel. So you should increase the players's mass.
If you go in a ferris wheel and make it spin fast enough, you will float too while going down. Setting restitution to 0 is good to avoid bounce, but it won't help keeping a light object in place: it's just not falling fast enough.
Also, you might want to actually increase the friction so the body won't slide. With 0 friction it's like sitting on ice.
Manually adjust your player's position after physics calculations.. in didSimulatePhysics:
if player.isOnPlatform {
player.position.y = platform.position.y // Maybe +1 or something like that
}
Now just add a contact delegate to toggle on / off when you make contact with the platform in didBegin(contact:
if player.position.y > platform.position.y { // So jumping under the platform wont warp you to the top
player.isOnPlatform = true // Make sure to toggle this off at the right times as well, such as in you player.jump() method.
}
You will have to do the bitmasks etc and tweak it as other issues may come up, but this a good approach I think without affecting gravity / downward forces on player which will mess up the feel of the game when you are on a platform.
Setting the restitution to -1.0 on the hero and the platform solved this issue for me. This is in conflict with the documentation for restitution which says: "The property must be a value between 0.0 and 1.0"
I am making a small SpriteKit game in Swift playgrounds and it is an asteroid defence type of game. Basically,the player is defending the spacecraft with a cannon and has to destroy approaching rocks. The image link below is my scene without the gun added yet, but basically I want the rocks to move slowly down from the top towards the spacecraft at the bottom (Sorry, it's too big to add in here).
https://www.dropbox.com/s/pisgazar4jaxra0/Screenshot%202017-03-18%2011.23.17.png?dl=0
The rocks will be SKSpriteNodes and I want to speed them up for each successive rock to increase difficulty over time.
How would I be able to implement this. I have seen some answers elsewhere about using UIBezierPaths but how do I make it random but with the constraint that the rocks only fall from above?
Thanks in advance!
If you only need them to go from top to bottom without any special curvy trajectory then you don't need to deal with splines.
Let's address the gradual acceleration first. You want them to fall slowly in the beginning and speed up during game progress. One approach would be to do track level time and do some duration math based on elapsed level time. An easier one would be to decrease the duration of time taken by the asteroid to reach the bottom. In your scene declaration section, declare those:
fileprivate let startingDuration: TimeInterval = 6.0 // Put here whatever value you feel fit for the level start falling speed
fileprivate let difficultyIncrease: TimeInterval = 0.05 // Put here whatever value you feel fit for how much should each next asteroid accelerate
fileprivate let collisionPosition = CGPoint(x: 300, y: 200) // Put here the onscreen location where the asteroids should be heading to
fileprivate var asteroidCounter = 0 // Increase this counter every time you spawn a new asteroid
Now, let's get down to the actual asteroid movement. There are different ways to approach it. The easiest one and recommended by Apple is to use an action for that. Add this code to your asteroid spawn method:
// Implement here your spawning code ...
// It's best to choose random starting points (X axis) a bit higher than the upper screen boundary
let fallingDuration: TimeInterval = startingDuration - difficultyIncrease * TimeInterval(asteroidCounter)
let fallingAction = SKAction.move(to: collisionPosition, duration: fallingDuration)
asteroid.runAction(fallingAction)
If you need a function for random integer generation, try this:
func RandomInt(min: Int, max: Int) -> Int {
return Int(arc4random_uniform(UInt32(max-(min-1)))) + min
}
Ok I'm developing a game in which you land a rocket ship while avoiding asteroids... Pretty simple. I have two UI Text elements that are meant to display the current vertical and horizontal velocity. I searched around and came up empty in terms of unity having a built in function to get the current velocity of a 2D rigidbody so I came up with this:
void Update () {
shipVelocity = Mathf.Abs((lastPosition.y - spaceShip.transform.position.y) / Time.deltaTime);
shipAngularVelocity = Mathf.Abs((lastPosition.x - spaceShip.transform.position.x) / Time.deltaTime);
uiText = "Vertical Velocity: " + shipVelocity;
currentVelocityText.text = uiText;
uiText = "Horizontal Velocity: " + shipAngularVelocity;
currentAngularVelocityText.text = uiText;
lastPosition = spaceShip.transform.position;
}
Basically it just does your standard velocity = delta position / delta time but something is wrong. The numbers bounce all over the place and after the ship has landed and is completely stationary the numbers still bounce around from 0-1. While it's flying the numbers rapidly change from what I think is an accurate velocity to 0 and back - so fast it basically just looks like it's blinking.
Could it be it's just calling it too fast? I.e. frame rate too high? If so how can I slow it down?
Thnx!
If you are updating a Rigidbody, it is important to put your update logic in "FixedUpdate()". This version of update is called at a set rate (default 50x/sec) and therefore should be used for updating physics logic. Link: https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
This is common in games as it separates the framerate (draws/sec) from the update logic (updates/sec), meaning that of the framerate drops, the games underlying physics are not altered.
If I've got a dude at the top of my screen, and a dude who comes in later at the bottom, and I want them both to move to the left of the screen using my moveToLeftOfScreen action I've created, how do I do that?
If I've got var moveToLeftOfScreen = SKAction.moveTo(CGPoint(x:blhabhla, y:bhlahbla, duration: 3)) that won't allow something to move to the left in a straight line from where it spawns.
Long story short: Can you make a single action that says "Move to the left until you hit the screen, then move back to the right until you hit the screeN" and apply that to things spawning in at different locations at different times and then just apply that action when needed throughout your code instead of writing a different action for every single height.
Contrary to sangony, I would have thought this is very possible, though I may be missing something.
Could you not create a reference to an SKAction that is a sequence of move to X=0 and move to X=size.width? and repeat if forever.
I'm not near my mac so I cant get the syntax perfect in XCode so apologies for any errors. Also this assumes that the scene will create this reference, hence the self.size.width:
let moveLeft = SKAction.moveToX(0, duration: 3)
let moveRight = SKAction.moveToX(self.size.width, duration: 3)
let moveLeftThenRight = SKAction.sequence([moveLeft, moveRight])
let continousMovement = SKAction.repeatActionForever(moveLeftThenRight)
Apply that final action to any node that spawns and you should be ok if I understand you correctly.
There will be an issue with the first movement being slow as it will always take 3 seconds to move to the left regardless of where the node spawns. This can be fixed by getting the fraction of the distance it needs to move and multiplying it by 3:
let firstMovementDuration = (node.position.x / self.size.width) * 3
let firstMoveLeft = SKAction.moveToX(0, duration: firstMovementDuration)
And from there continue with normal movement. There's also the timingMode to consider for more natural movement such as .EaseInEaseOut.