Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Collision checking without the need of a rigidbody

Discussion in 'Scripting' started by wetfiregames, Jun 12, 2014.

?

Should a new system for Collision checking without the need of a rigidbody be implemented?

  1. Yes

    40.0%
  2. No

    60.0%
  1. wetfiregames

    wetfiregames

    Joined:
    Nov 17, 2012
    Posts:
    28
    So I have a basic concept for swimming on the surface of water that involves a child Collider as a small Sphere positioned on the 'neck' in-between the characters shoulder blades . This would allow for any 'height' of any character the ability walk into the water and when the water level reaches their 'neck' it would simply use the physics engine to keep the character from sinking below an invisible 'water surface collider' just slightly below the waters surface. I use the method call 'OnCollisionStay' in my script on the 'neck' collider to notify another script attached to the parent object of the character that starts the swimming animation.

    The Problem:
    - The OnCollisionStay requires a 'rigidbody' in order for it to be called. Of course the character model already has a rigidbody, so in this case adding another rigidbody to the 'neck collider' does not make sense as now to rigidbodys are acting independently with the physics engine (drag / gravity / etc..).
    - If the OnCollisionStay method didn't need the gameobject to require a rigidbody then this would not be an issue for this particular setup.
    - Obviously making either of the two colliders a trigger or setting them to kinematic, would cause the objects to pass through one another.

    Possible fix (But seems needlessly complicated):
    -Duplicating the 'neck' and adding another sphere collider as a trigger, then passing the boolean variable of the OnTriggerStay funciton attached to that collider to the main player script that enacts the mecanim 'swim' animation. Thus allowing me to keep the other sphere collider. I feel like there would be a better way or maybe I am way over thinking things here. Maybe this will help others out.

    Any ideas or Alternatives?
    (Test Scene photo attached below, the collision with the neck happens at the white flat surface)

    WaterCollision.png
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    I think you are over complicating your solution. Assuming the water level is dynamic, why not just raycast onto the water to determine the distance between the character and the surface. If the distance is too great, you add an upward force.
     
  3. wetfiregames

    wetfiregames

    Joined:
    Nov 17, 2012
    Posts:
    28
    I'll give it a shot, I feel that it would push the character out of the water depending on the mass and gravity force acting on the rigidbody of the character, Is there perhaps a way to align the transform of the player neck to the 'y' transform of the water collider?
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    The head should go out of the water a bit, that happens in reality as well. If you have gravity turned on, the counter force should be great enough to counter the upward force once the head is out of the water.
     
    wetfiregames likes this.
  5. wetfiregames

    wetfiregames

    Joined:
    Nov 17, 2012
    Posts:
    28
    Thanks for the suggestion I have found a 'hover' script on the forum and was able to utilize the ray-cast as you recommended.