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

Pushing a block of ice

Discussion in 'Physics' started by Neo_Genesis10, Nov 15, 2017.

  1. Neo_Genesis10

    Neo_Genesis10

    Joined:
    Feb 20, 2017
    Posts:
    5
    This one is rather specific, but I seem to keep hitting snags along the way. It seems to me that this should be easy to do, but apparently that is not the case.

    Here's the scenario. I have a block made of ice (rigidbody). I want the player to be able to push this block on a fixed axis (X or Z), and the block is affected by gravity. Now, in traditional video game style, I want this block to be essentially frictionless. You push it, and it keeps sliding until it hits something solid to stop it in its tracks.

    Now the problems. Firstly, there's no easy way to restrict the resulting movement to a single axis. Secondly, there's no easy way to keep the block at speed. Gravity seems to be adding drag even with drag set to zero.

    Any ideas on how to solve this conundrum?
     
  2. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    709
    Have you looked at Physic Material?

    The two parameters I see that may be of interest:

    Dynamic Friction The friction used when already moving. Usually a value from 0 to 1. A value of zero feels like ice, a value of 1 will make it come to rest very quickly unless a lot of force or gravity pushes the object.

    Static Friction The friction used when an object is laying still on a surface. Usually a value from 0 to 1. A value of zero feels like ice, a value of 1 will make it very hard to get the object moving.
     
  3. Neo_Genesis10

    Neo_Genesis10

    Joined:
    Feb 20, 2017
    Posts:
    5
    That's the part I can't figure out. Dynamic Friction is set to zero while Static Friction is set to a low 0.1. I think the problem might be in the code I'm using to ensure it only moves on a single X/Z axis (I want no diagonal movements unless it involves gravity). The issue is that sometimes it'll keep going without stopping and sometimes it comes to a halt abruptly. Sometimes it doesn't even move at all!

    Here's the code I'm using. I call it during OnCollisionEnter so that I can catch the velocity before the object gets pushed.

    Code (csharp):
    1.  
    2.     private void OnCollisionEnter(Collision collision)
    3.     {
    4.         Vector3 Xvector = new Vector3(rbody.velocity.x, 0, 0);
    5.         Vector3 Zvector = new Vector3(0, 0, rbody.velocity.z);
    6.         if (collision.collider.tag == "Scenery")
    7.         {
    8.             return;                                     // Solid unmovable objects can be ignored
    9.         }
    10.         else if (collision.collider.tag != "Player")
    11.         {
    12.             rbody.velocity = new Vector3(0, rbody.velocity.y, 0);
    13.             return;
    14.         }
    15.         else
    16.         {
    17.             if (Xvector.magnitude > Zvector.magnitude)
    18.             {
    19.                 rbody.velocity = new Vector3(slideSpeed, rbody.velocity.y, 0);
    20.             }
    21.             else if (Xvector.magnitude < Zvector.magnitude)
    22.             {
    23.                 rbody.velocity = new Vector3(0, rbody.velocity.y, slideSpeed);
    24.             }
    25.             else if (Xvector.magnitude == Zvector.magnitude)
    26.             {
    27.                 rbody.velocity = new Vector3(0, rbody.velocity.y, 0);
    28.             }
    29.         }
    30.     }
    It's not the prettiest solution, but the block does at least move on a fixed axis. If there is a more elegant solution, I'm more than happy to try it.