Domanda

I setup a hero and some platforms that are moving from the top downwards. With these I have collisionBitMasks that detect when the hero lands on a platform if the hero comes from above (to let the hero jump through the platforms)

if (_hero.physicsBody.velocity.dy > 0) {
                            _hero.physicsBody.collisionBitMask = 0;
                        }
                        else {_hero.physicsBody.collisionBitMask = platformCategory;
                                }

Everything works fine, except that the hero keeps bouncing on the platform. Is there a way to let him sit on it, while the platform is moving down?

I tried using physicsBody.resting and physicsBody.friction, but without any success.

Thanks for help Guys

È stato utile?

Soluzione

Had the same issue just a minute ago. It works with setting the restitution but you need to set both restitutions. The one of the hero AND the one of the platform (or in my case the scene boundaries).

so:

hero.physicsBody.collisionBitMask = platformCategory
hero.physicsBody.restitution = 0.0
platform.physicsBody.restitution = 0.0
any_other_object_that_should_still_bounce.physicsBody.restitution = 1.0

will do it. All other objects on the screen still bounce on the platform as long as you set their restitution > 0

Altri suggerimenti

The trick is to avoid any physics behavior while the hero is on the platform by resetting the hero body's velocity and setting the hero's position to a fixed vertical offset from the platform's position.

In semi-pseudo-code:

-(void) didSimulatePhysics
{
    if (<hero on platform>)
    {
        hero.physicsBody.velocity = CGPointZero;
        hero.position = CGPointMake(hero.position.x, 
                                    platform.position.y + <offset as needed>);
    }
}

Have you tried altering the restitution property of the nodes' physics bodies?

_hero.physicsbody.restitution = 0;

It controls the bounciness...

You can use the Collision category:

When the velocity.dy is < 0 the hero is falling.

func collisionWithPlayer(player: SKNode) {
    // 1
    if player.physicsBody?.velocity.dy < 0 && (player.position.y - player.frame.size.height / 2.0) >= (self.position.y){
        // 2
        player.physicsBody?.collisionBitMask = CollisionCategoryBitMask.Platform

    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top