Search Unity

Prevent collision physics effect

Discussion in 'Scripting' started by Rhuantavan, Aug 10, 2006.

  1. Rhuantavan

    Rhuantavan

    Joined:
    Feb 23, 2006
    Posts:
    53
    In the platformer I am making I don't use the character controller but I need to detect collisions between the player character and the world (platforms). Both my character and the world's platforms have a rigidbody attached and to be able to generate the OnCollisionEnter event I have set the player character to be kinematic (I move it the old-school way - that is without the physics engine).

    The problem is that when the character bumps into some platform, the platform moves (about) the amount of the collision penetration between the rigidbodies. Well I need the character to stop before the platform and prevent the platforms to move... any suggestions? Thanks!
     
  2. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Maybe make an invisible collider slightly bigger than the character, and if that hits, refuse to move the character?
     
  3. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    Actually there's a slightly easier fix. If your platforms are moving around due to physics interaction, then they must have rigidbodies attached. In the rigidbody component of your environment pieces, enable the checkbox that says "Is Kinematic".

    That should do the trick. However, if you want to move them physically through scripting, you will have to disable isKinematic via your script.
     
  4. Rhuantavan

    Rhuantavan

    Joined:
    Feb 23, 2006
    Posts:
    53
    Unfortunately that doesn't work because I already have turned on the IsKinematic property for the character (the first collider)... from the manual: "Note that collision events are only sent if one of the colliders also has a non-kinematic rigid body attached."
    Am I stuck?
     
  5. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Have you looked at setting one of the colliders to a Trigger and using OnTriggerEnter()?
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You want to create a trigger collider and attach it as a child of the character. Maybe just a small trigger sphere at the feet of your character. With OnTriggerEnter you get all events when you bump into another character.

    The other possibly simpler way to solve it is just changing the mass of the character or platform. So the platform is much heavier than the character, thus wont be affected much when the character jumps on to it.
     
  7. Rhuantavan

    Rhuantavan

    Joined:
    Feb 23, 2006
    Posts:
    53
    I am on that way it seems.

    So can I create many triggers and put them in strategic places to detect various collisions (feet on ground, body against wall, etc...)? Is this a good strategy? I was hoping I could use the OnCollisionEnter event to get more info on the collision to detect where the collision took place, but I guess I can obtain pretty much the same info with triggers. Am I in the right direction?

    Thanks everyone for helping.
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can place the triggers at important places to figure out what part collides. of course you can use OnCollisionEnter which tells you exactly where you collide, but that is after the fact. So your colliders already applied collision response. Pushing other rigidbodies etc.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I wonder if it would be worth just biting the bullet and making the character controller non-kinematic. It would take rather more effort to get the movement right, but you could make all the platforms kinematic so you won't have this issue at all, plus it would open up the possibilities for having various physics effects on the character if you wanted to get fancy.

    --Eric
     
  10. Rhuantavan

    Rhuantavan

    Joined:
    Feb 23, 2006
    Posts:
    53
    I don't know on that one Eric. I am trying to keep the game as simple as possible, just to finish a project once for a change :) I have been thinking on making everything physics based, but that would probably be an overkill for my precious spare time working on the project.

    Still, I believe I can simulate most of the physics anyway... I have to think about it.