Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Platform pushes then reverses direction

Discussion in 'Physics' started by KamiKaze425, Feb 13, 2015.

  1. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    Hey guys,
    So I have a moving platform. It will go back and forth on a set path (works fine). If it collides with an object, it will just push it. However, if it cannot push it any further (say a wall stops it), then the platform will reverse direction.
    I'm using rigidbodies for this. My main issue is overcoming the bounce when it can't push something any further.
    I got a bit close, I make sure my trajectory is maintained. But the transform of the the platform shifts slightly.
    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.  
    4.         if((thisTransform.position - startPosition).magnitude > travelDistance)
    5.         {
    6.             thisRigidbody.velocity = -thisRigidbody.velocity;
    7.         }
    8.         Vector3 newVel = Vector3.zero;
    9.         bool flip = false;
    10.         if((thisRigidbody.velocity.x >= 0 && direction.x >= 0) || (thisRigidbody.velocity.x < 0 && direction.x < 0))
    11.         {
    12.             newVel.x = direction.x;
    13.         }
    14.         else
    15.         {
    16.             flip = true;
    17.         }
    18.         if((thisRigidbody.velocity.y >= 0 && direction.y >= 0) || (thisRigidbody.velocity.y < 0 && direction.y < 0))
    19.         {
    20.             newVel.y = direction.y;
    21.         }
    22.         else
    23.         {
    24.             flip = true;
    25.         }
    26.         if(flip)
    27.         {
    28.             newVel = -direction;
    29.             direction = -direction;
    30.         }
    31.         thisRigidbody.velocity = newVel.normalized * speed;
    32.     }
    My drags are both set to 0. I'm sure I can optimize that a bit. But I've been playing around with making it work first, haha.

    Here is the effect I'm aiming for:
    Starts off like this. The black platform will move back and forth from its location to just right before the horizontal white stationary platform to its right.
    example.PNG

    The green block will fall between the black moving platform and the white stationary platform. The moving platform will push the green platform to the right as far as it can. Essentially altering the max distance the moving platform can travel to the right.
    example2.PNG

    Now, I can also have the moving platform rotated with a diagonal path. And then the transform shift and angular bounce become an issue.

    Thanks for any advice!