- Home /
How to move a character in front of a collider and be able to jump and be on top of the said collider?
Hi,
I don't know if this has been answered here before but I would like to know how to move a character in front of a collider and be able to jump and be on top of the said collider. With the example below I want my character to move in front of the house but be able to
a) walk up the stairs
b) just jump on the balcony? Help?
Answer by Llama_w_2Ls · Jul 02, 2021 at 02:08 PM
One way you can do this is by setting the collider to IsTrigger, and setting it to a non-trigger collider if the player is on top of it. To detect whether the player is above the collider, use a raycast that points downward, and turn off IsTrigger for any colliders that it hits. For example:
void Update()
{
ActivatePlatformGround();
}
void ActivatePlatformGround()
{
var hitInfo = Physics2D.Raycast(transform.position, Vector2.down);
hitInfo.collider.isTrigger = false;
}
Note: All platforms in the scene need to be set back to IsTrigger if the player isn't above it. You can add a script to the platform to set itself to IsTrigger every LateUpdate or smt.
Thank you!
I'm really new to all of this, program$$anonymous$$g with C# and Unity, as a whole, I am curious why write it in code versus using a PlatformEffector2d component?
Yes, I originally wrote the question for a 3D and 2D solution, but for a 2D game like yours, a platform effector 2D works perfectly for this.
Your answer
Follow this Question
Related Questions
Character animation jerks when moving 1 Answer
Unity 2D player sticks on platform corners 2 Answers
Rigidbody2D.AddRelativeForce not working 1 Answer
2d Tilemaps, pass through some objects 1 Answer
[2Dplatformer][Problem] When the player closes to the enemy, the enemy pass through the colliders 0 Answers