I have a battle system where the players and enemy have character controllers. However, if a player jumps on top of an enemy, he can stay on top of it. I want to know if there is a way to do the following :
I think that you could easily do this with a trigger collider on top of the enemy. Just put a box-shaped trigger as a child of the enemy on top of its head (slightly above, so that the player would collide with it when on top of the enemy). While the player is in collision with the trigger, just move him. The trigger would have a script like this:
var slideSpeed : float;
//While the player is in collision with the trigger, move him forward
It's kind of a simple solution, but it should slide the player off of the enemy's head effectively.
answered Feb 14, 2011 at 01:53 AM
If both the player and monster are basing their collisions on capsule colliders as they should be with character controllers, I should think this would happen naturally... or should.
In the event that it's not, as I'm sure it isn't, you should run OnCollision checks and get your character collider's hit normals (you can debug this by running the aforementioned check and then within that function, send the hit.normal to a GUIText or something). Based on the hit normals, it should be pretty easy to apply dynamic velocity until the player is completely slid off the monster's head (since hit normals are just vectors, with each axis ranging from -1.0 to 1.0).
Basically what I'm saying is, grab the hit normals and do some basic math to figure out an ideal way to apply force to the character using the normal values to push him off. I'm not sure on the math required to do it, but I hope that helps you brainstorm something.
answered Dec 28, 2010 at 01:42 AM
Sorry, I had to add the code as a second answer. I realized my usual login isn't registered, so I couldn't use it on a different computer, and just decided to properly register a new ID this time (and lose my precious few medals, blah!).
Here's a variable I would use to grab and hold onto the last known collision normal.
Here's the code I use to grab hit normals of an attached CharacterCollider.
Any time the character controller collides, it will report the hot normals to my GUI Text.
Debug_Text is just a variable I have declared at the top of my script.
It's public so I can just drag and drop the GUI Text into it (I make most of my variables public just out of laziness!).
Okay. So if you play with that, you'll see on-screen what the hit normals look like whenever the attached controller collides with something. What you want to pay attention to is the X and Z coordinates. They'll range from -1.0F to 1.0F depending what angle they're colliding with the monsters.
Applying dynamic motion is now stupid-simple from here. :D Plop this single line of code wherever you want the motion to occur (probably inside an Update). I use SimpleMove rather than Move because in this case you probably want gravity to take effect. If you don't, use Move instead.
What that code does: Finds the CharacterController attached to the game object that this script is attached to. Orders movement. Orders it to move in the direction of the hit normal X value (stored in hitNormal.x), no Y value, and the hit normal Z value (stored in hitNormal.z).
Is that more helpful?
answered Dec 28, 2010 at 08:59 PM
you can try using a trigger collider than sends a sideways force certain tags such that when the "green egg" ( refer to own picture ) when it collides with the fake collider of the " red egg". this ensures smooth sliding of the "green egg" when colliding with the "red egg". Also, check the trigger collider for info on fake colliders. Good luck!
answered Feb 11, 2011 at 07:37 AM