Search Unity

Moving Platforms and Physics questions

Discussion in 'Physics' started by konsic, May 1, 2019.

  1. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    Issue
    I've stumbled across this issue with moving platform and player standing on it. It stutters and falls of the platform.
    Platform traverse with Time.time .

    It can be solved by parenting the player but then player's transform is locked for that moment and player's parent needs to be set to null when leaving the platform.

    I think this is related to how colliders operate with physics along time.
    I hadn't seen this issue in Unreal. When player steps on platform, it travels along and doesn't have to get parented with static mesh.

    Questions:
    1. Is it possible to get similar thing in Unity so that player can move with the platform without being parented?
    2. Is it possible to move the platform related with Time.deltaTime ? (The thing is, if I use Time.deltaTime platform doesn't move. )
    3. Can I use different physics solution in Unity where colliders wouldn't depend to Time.time parameter?
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Try enabling IsKinematic in the platform's rigidbody, then move it with Rigidbody.MovePosition within FixedUpdate. Using Time.deltaTime for the calculations is ok, something like:

    newPos = rigidbody.position + velocity * Time.deltaTime;
    rigidbody.MovePosition(newPos);
     
  3. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    I didn't set rigidbody for platform. Only box collider.
    I have only char contolller too so I thought I don't need it.

    I did try to set Time.deltaTime and Time.SmoothdeltaTime but then platform doesn't move.
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    If it doesn't move then either velocity is zero, or there's something wrong in your setup.
     
    konsic likes this.
  5. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    Of course, the solution depends on the "movement environment", are you using dynamic rigidbodies? kinematic rigidbodies? pure transforms? a mix of those? I don't know Unreal Engine, but this feature does not come for free, they probably scripted that for its character controller component.

    In general the solution is quite logical, you will have to apply the effects of the platform onto the character (whether is velocity, rotation, or whatever you want) before the usual movement stuff you are doing frame by frame, like walking, jumping, etc. (You will need to record the movement/rotation changes of the platform in order to do this).

    Yes, just define your deltaPosition vector, for example:
    Code (CSharp):
    1. Vector3 deltaPosition = speed * dt;
    Then use a proper method to do the movement (based on the movement environment i mentioned before):
    Code (CSharp):
    1. rb.MovePosition( rb.position + deltaPosition );
    2.  
    3. rb.position += deltaPosition;
    4.  
    5. Transform.Translate( deltaPosition );


    Your "time" problem is probably due to the difference between Update vs FixedUpdate + the movement method you've chosen. The order of execution of every gameObject (update + fixedUpdate) in Unity is a tricky subject, i don't think the physics system has nothing to do with the problem (at least at a first glance).

    Please post your movement code.
     
    konsic likes this.
  6. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    lightbug14, I'm using only character controller, but I'm not sure if I should use it due to collisions not triggering with other objects.
    Somethimes they trigger, sometimes they don't as if it's a bug.

    Code (CSharp):
    1. transform.position = new Vector3 (1,32*Mathf.Cos(Time.time)+5,1);
     
  7. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    Is that the Unity character controller component? If so try to do the movement with the Move method instead of changing directly the position. I believe the character controller can activate a trigger by it own (without collider), i'm not 100% sure.

    Code (CSharp):
    1. Vector3 targetPos = new Vector3 (1,32*Mathf.Cos(Time.time)+5,1);
    2.  
    3. Vector3 deltaPos = targetPos  - transform.position;
    4.  
    5. characterController.Move( deltaPos );
    It would help if you post more code (platform and character) and a more detailed setup of your scene.
     
    konsic likes this.
  8. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    Yes, standard Unity's CC asset.



    I will try this.
     
  9. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Hey Lightbug, I saw that you're doing a whole package for 2d so I'm thinking you must know your way around moving platforms :)

    I have a problem with moving platforms in my 3D game that I wrote on this thread:
    https://forum.unity.com/threads/blocked-moving-platform-still-makes-passenger-move.709871/

    If you got some ideas on how to tackle this problem that'd be really cool.