- Home /
2D platformer. Let player slide under object.
I am making a 2D platform.
There are 2 types of ground player can land on. One is normal on which a player can stand. The other one is like a rope. Player can hold it and just slide.
What would be best way to implement rope type of ground.
I have two ideas in mind
Idea 1) Create a collider under the actual rope sprite and add sliding animation. Player stands on the lower collider. Idea 2) Let rope have a collider. When player collides with rope, set gravity scale of player to 0. When collision exit, set gravity scale 1.
Any other method of producing result?
Also, currently I have added a collider under grounds which when triggers, it sets players collision to disable so player can pass through ground is he jumps up. And another collider just above ground, that re enables player collider. This sort of works like one directional collider.
Is there any easier implementation for this one directional collider.?
EDIT :
Sliding :
][1]
By slide, you mean to the side? you also said rope, so up or down? and is the sliding or climbing done automatically, manually (button click) or done step by step, like the player climbs a rope by holding up and if stopped he'll stop and continue only on button click again?
Well, I don't know exactly your game, so things might not be right for you. I once did the same thing with just attaching a child with a collider and a rigidbody, above the player, slightly above the head, making the collider collider but making the player avoid the collision which will make him in the bottom. You can also add transform position. transform.position += new Vector3(2,-2,0) making it go 2 to right and 2 left.
Problem would be that once player collides, he would get pulled by gravity ins$$anonymous$$d sliding under rope.
Why would he? if you've got a collider above him, then it should hold him. The collider should make the player resist the gravity. make sure it's a collider and not a trigger. and that the empty gameObject with the collider above the player attached to the player.
Seems that the only thing colliders do is "collide". When player hit's them, the player stops and is now pulled by gravity. I think I should just turn off gravity when player enters collider and turn it back on when he exits.
Answer by Immanuel-Scholz · Jun 30, 2015 at 08:22 PM
I would definetely go with Idea 1.
If you disable gravity and let the player "bounce" against the rope, you get strange results when the slope increase/decrease. Beside, why mess around with the physics world if you don't have to?
There is a third solution that fits well, if you have an explicit user-driven switch between rope sliding and normal walking (e.g. user needs to press a key): You actually have two collider on your player. One big normal collider that spans the whole body for movement and a second, very small circular collider where his hands are during the slide animation (in the upper-right corner of the normal character sprite area).
The rope itself has a collider that just spans the rope (it may go a bit below the rope, but not necessarily too much).
When you change to the slide state, disable the big and enable the small collider. If your character is positioned correctly, he will naturally "attach" to the rope since his hands are now on the other side of the rope. And he will naturally slide down the rope.
Be sure to assign correct layers to the rope collider to not have it collide with the main body collider or else the player may snap out of place when you switch back (and is generally unable to walk through a rope).
Here is how I did this.
1) Created new gameobject and added sprite and a hook as child. 2) Hook position is just above sprite. 3) Added rigidbody and script to player. 4) Added 2 colliders near rope and attached them as child to an empty gameobject. One lower and one upper. Both are triggers
When something collides with LowerCollider, it checks it is hook. If collided object is hook, then set hook collider to OnTrigger so that hook can go above the rope.
void OnTriggerEnter2D(Collider2D collide){
if (collide.name == "Hook") {
collide.isTrigger = true;
}
}
When hook collides with upper collider, set OnTrigger to false.
void OnTriggerEnter2D(Collider2D collide){
if (collide.name == "Hook") {
collide.isTrigger = false;
}
}
Now three things are happening
1) Player sprite have big collider, so it can't go above rope. 2) Hook collider is set to collide and not trigger. 3) Gravity is pulling player down.
So, hook will stick to top of rope. In game it would feel like player is hanging, but ins$$anonymous$$d, it is hook which is hanging.