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

Help With Unity Physics...

Discussion in 'Editor & General Support' started by Nakel, Aug 21, 2020.

  1. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    I know this is a really simple question... but I would really appreciate it if someone helps me.

    I'm trying to move rigidbodies that are placed above of a box collider. These rigidbodies remain in the same position ignoring that the object below is moving...

    -All rigidbodies have gravity enabled.
    -I've tried adding rigidbody to the object below as well.

    https://gifyu.com/image/ctUG

    I have succesfully made that by making the big box parent of the others...

    https://gifyu.com/image/ctUy

    Is there any other way???
    Thanks!
     
  2. Rodi-Bro

    Rodi-Bro

    Joined:
    Jun 6, 2016
    Posts:
    3
    I think what you do is the right way. If you are going to move the player from one moving platform to another moving or non-moving platform you have to set the player as child of the platform. This is the script i attach to the platforms. It sets the player as child of the platform upon collision and sets it back to normal upon leaving the platform.
    Code (CSharp):
    1. public class PlatformScript : MonoBehaviour
    2. {
    3.     void OnCollisionEnter(Collision collision)
    4.     {
    5.         if (collision.gameObject.tag == "Player")
    6.         {
    7.             collision.collider.transform.SetParent(transform);
    8.         }
    9.     }
    10.     void OnCollisionExit(Collision collision)
    11.     {
    12.         if (collision.gameObject.tag == "Player")
    13.         {
    14.             collision.collider.transform.SetParent(null);
    15.         }
    16.     }
    17. }
     
    Nakel likes this.
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    I don't use parenting for this sort of thing. I'm not saying it's not a viable approach, but I felt it was a bit messy, and raised concerns about walking from one moving platform to another moving platform (you can't have two parents).

    As long as I move the platform is kinematic and I'm moving it using rigidbody.MovePosition(), I haven't had any issues with objects staying on the platform. If the platform goes very quickly (10m/s or so), things can slide around on the platform a bit, but that's probably pretty realistic. I guess it depends on whether you want things to behave realistically (where the objects on the platform have some inertia) or you want them to be glued to the platform.
     
    Nakel likes this.
  4. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    Could you please share an example of this :)
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Well, my moving platform has a box collider on it, and a kinematic rigidbody:

    upload_2020-8-22_1-53-27.png

    (Note: The mass here probably makes no difference.)

    Then, I just change its position with MovePosition, like this:
    rigidbody.MovePosition(newPosition);


    As long as the other things sitting on the platform are non-kinematic rigidbodies, they should just go along for the ride as the platform moves.
     
    Nakel likes this.
  6. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    After googling and found a cool method...

    Code (CSharp):
    1. {
    2.     public List<Rigidbody> rigidbodies = new List<Rigidbody>();
    3.     Transform _transform;
    4.     public Vector3 LastPosition;
    5.  
    6.     private void Start()
    7.     {
    8.         _transform = transform;
    9.         LastPosition = _transform.position;
    10.     }
    11.  
    12.     void LateUpdate()
    13.     {
    14.         if (rigidbodies.Count > 0)
    15.         {
    16.             for (int i = 0; i < rigidbodies.Count; i++)
    17.             {
    18.                 Rigidbody rb = rigidbodies[i];
    19.                 Vector3 velocity = (_transform.position - LastPosition);
    20.                 rb.transform.Translate(velocity, _transform);
    21.                 //Vector3.Lerp(_transform.position, LastPosition, Mathf.Cos(Time.time / travelTime * Mathf.PI * 2) * -.5f + .5f);
    22.             }
    23.         }
    24.         LastPosition = _transform.position;
    25.     }
    26.  
    27.     private void OnTriggerEnter(Collider collision)
    28.     {
    29.         Rigidbody rb = collision.GetComponent<Rigidbody>();
    30.         if (rb != null && rb.gameObject.layer != 9)
    31.         {
    32.             Add(rb);
    33.         }
    34.     }
    35.  
    36.     private void OnTriggerExit(Collider collision)
    37.     {
    38.         Rigidbody rb = collision.GetComponent<Collider>().GetComponent<Rigidbody>();
    39.         if (rb != null)
    40.         {
    41.             Remove(rb);
    42.         }
    43.     }
    44.  
    45.     void Add(Rigidbody rb)
    46.     {
    47.         if (!rigidbodies.Contains(rb))
    48.         {
    49.             rigidbodies.Add(rb);
    50.         }
    51.     }
    52.     void Remove(Rigidbody rb)
    53.     {
    54.         if (rigidbodies.Contains(rb))
    55.         {
    56.             rigidbodies.Remove(rb);
    57.         }
    58.     }
    Thank you very much!