prime31 / CharacterController2D

1.07k stars 248 forks source link

Character can't push boxes, blocks which have rigidbody or not. How can they push them? #65

Closed bekiryanik closed 7 years ago

bekiryanik commented 8 years ago

Hello,

First of all i would like to thank you for that great character controller project. Really appreciated. It's awesome. I have a little problem. When i try to push a box or block they both same, i couldn't push them with my character. I tried to edit the script but it seems really complicated. Can you show me a why how to add it or fix it?

I will be waiting for your reply. Thanks a lot. Have a great day!

bekiryanik commented 8 years ago

Any idea?

jamiltron commented 8 years ago

This is somewhat non-trivial and its very dependent on what you mean by "push."

The way I typically do this in platforming games is to make a behavior script, called something like "Pushable" that has a reference to the CharacterController2D attached to the pushable object.

In your Pushable script you register a method to the CharacterController2D's OnControllerCollidedEvent that checks where its being pushed from (typically the origin of who its colliding with), how far it needs to go and in what direction (typically the velocity of who its colliding with), if the thing that is colliding with it can push it at all (often based on a tag or even a Component that the pusher has), and any other conditions you may have.

You then attach this Pushable behavior script to anything that can be pushed.

This is a really naive solution, and any implementation is going to be pretty dependent on your game and needs.