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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

falling through

Discussion in 'Editor & General Support' started by fombrot, Nov 14, 2007.

  1. fombrot

    fombrot

    Joined:
    Jul 5, 2007
    Posts:
    16
    hello. i am trying to make a model to move up and down with the unity animation and for some reason when my character will not collide.
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hi.

    Please could you explain this a little bit better? I personally cant quite follow what you mean..

    Cheers
    AC
     
  3. fombrot

    fombrot

    Joined:
    Jul 5, 2007
    Posts:
    16
    sorry i was in a hurry. i am making a object with a mesh or box collider move up and down and i need the character to stand on it. but when it starts moving he falls through.
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Theres been a few issues with people implementing this in the past.

    http://forum.unity3d.com/viewtopic.php?t=1092

    I havent come up against this problem yet so I cant really help you. There is a package at this link that may help, and lots of posts with code etc.

    Hopefully thats a help. Further forum searching might yield more success.

    AC
     
  5. fombrot

    fombrot

    Joined:
    Jul 5, 2007
    Posts:
    16
    thank you for the link. but did not help.
     
  6. terransage

    terransage

    Joined:
    Jul 17, 2006
    Posts:
    290
    I'm not sure if this is the best solution, but it works for me: I place a trigger (parented to the platform) directly on top of the platform and have a parenting/unparenting script on the trigger. The First Person Controller steps onto the platform, becomes parented to it, stays with it as it rises and falls or moves sideways, etc.. Then, when the FPC steps off the platform, he's unparented from it and free to go on his merry way. Here's the code:
    Code (csharp):
    1.  
    2. function OnTriggerEnter () {
    3.    var GO = GameObject.Find("MovingPlatform");//whatever the name of your moving platform is
    4.    var GO1 = GameObject.Find("First Person Controller");
    5.  
    6.    GO1.transform.parent = GO.transform;    
    7. }
    8.  
    9. function OnTriggerExit () {
    10.    var GO = GameObject.Find("MovingPlatform");
    11.    var GO1 = GameObject.Find("First Person Controller");
    12.  
    13.       GO1.transform.parent = null;
    14. }
    15.  
    16.  
    Note: Make sure the skin of your First Person Controller capsule is thick enough (you have to experiment with that), or it will fall through. In my platforming code I also used triggers to get the animation of the platform started, but I didn't include that here. Good luck!

    Edit: If you're not using a first person controller, it should still work with an animated third person character. Just change the character name in the variable declaration.
     
  7. Jasper-Hesseling

    Jasper-Hesseling

    Joined:
    Jul 27, 2009
    Posts:
    34
    Thanks man that script helped me out m8.
     
  8. rozgo

    rozgo

    Joined:
    Feb 7, 2008
    Posts:
    158
    C# pseudo code, convert to javascript if needed

    this should work almost flawlessly for a CharacterController on a rigidbody moving platform

    Code (csharp):
    1.  
    2. Transform activePlatform;
    3. Vector3 activeLocalPlatformPoint;
    4. Vector3 activeGlobalPlatformPoint;
    5. Vector3 lastPlatformVelocity;
    6. CharacterController controller;
    7.  
    8. void OnControllerColliderHit (ControllerColliderHit hit) {
    9.     if (hit.moveDirection.y > 0.01f)
    10.         return;
    11.     if (hit.moveDirection.y < -0.9f  hit.normal.y > 0.9f) {
    12.         activePlatform = hit.collider.transform;
    13.     }
    14. }
    15.  
    16. void Update () {
    17.  
    18.     // relative movement
    19.     if (activePlatform != null) {
    20.         Vector3 newGlobalPlatformPoint =
    21.             activePlatform.TransformPoint(activeLocalPlatformPoint);
    22.         Vector3 moveDistance =
    23.             (newGlobalPlatformPoint - activeGlobalPlatformPoint);
    24.         transform.position = transform.position + moveDistance;
    25.         lastPlatformVelocity =
    26.             (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;
    27.     } else {
    28.         lastPlatformVelocity = Vector3.zero;   
    29.     }
    30.     activePlatform = null;
    31.    
    32.     // character moving code
    33.     Vector3 velocity = Physics.gravity * Time.deltaTime;
    34.     controller.Move(velocity);
    35.    
    36.     // localize movement
    37.     if (activePlatform != null) {
    38.         activeGlobalPlatformPoint = transform.position;
    39.         activeLocalPlatformPoint =
    40.             activePlatform.InverseTransformPoint (transform.position);
    41.     }
    42. }
    43.  
     
  9. Jasper-Hesseling

    Jasper-Hesseling

    Joined:
    Jul 27, 2009
    Posts:
    34
    This scripts works for me in an elevator setup when going up but I still get a falling sensation when I go down. Is there someway to work around that?

    Thanks.
     
  10. rozgo

    rozgo

    Joined:
    Feb 7, 2008
    Posts:
    158
    Parenting is not a solution to physics-based motion. Physics is best treated in the same frame of reference.

    Something similar to this (took this snippet from a backup, not reliable):


    Code (csharp):
    1.         void Update () {
    2.             Vector3 velocity = Physics.gravity * Time.deltaTime;
    3.             if (platform != null) {
    4.                 velocity = platform.velocity * Time.deltaTime;
    5.             }
    6.             controller.Move(velocity);
    7.  
    8.         }
    9.        
    10.         public override void OnControllerColliderHit (ControllerColliderHit hit) {
    11.             if (hit.moveDirection.y > 0.01f)
    12.                 return;
    13.             if (hit.moveDirection.y < -0.9f  hit.normal.y > 0.9f) {
    14.                 platform = hit.collider.rigidbody;
    15.             }
    16.         }
    There are a few things incomplete with this code, but basically, just take the velocity of the rigidbody he is standing on (or the delta position of a non dynamic object) and add it to your character. Also note this chunk of code is in Update, use Fixed instead. Anyway, I ended scrapping the CharacterController and rolled my own implementation based on rigidbody w/ capsule collider. This is not recommended for every situation, though.