1
votes

I am working on a pool game of sorts. To create a table, I have used cubes as sides. I want to use the inbuilt physics engine to get those sides interact with the balls. Sadly I am unable to get it working.

Here is what I have done. I created cube as side, and a sphere for ball. To move the sphere, I am using rigidbody.MovePosition function. both the cube and sphere have colliders and rigidbody attached, and gravity turned off.

Sphere movement is fine, but when it collides with cube, it makes the cube fly. Since the cube is supposed to be an immovable wall, I constrained all axes rotation and movement. But, using constraints cause physics engine to go bonkers. Instead of sphere coming to stop or moving in opposite direction, it simply passes through the cube. Clearly, something is wrong, and I need help figuring out the same.

Thanks in advance.

Here is the code used to move the sphere.

public float movePower = 10.0f;

// Update is called once per frame
void Update () 
{
    if(Input.GetKey(KeyCode.LeftArrow))
    {
        rigidbody.MovePosition(transform.position + Vector3.left* movePower * Time.deltaTime);
    }
    if(Input.GetKey(KeyCode.RightArrow))
    {
        rigidbody.MovePosition(transform.position + Vector3.right* movePower * Time.deltaTime);
    }
    if(Input.GetKey(KeyCode.DownArrow))
    {
        rigidbody.MovePosition(transform.position + Vector3.down* movePower * Time.deltaTime);
    }
    if(Input.GetKey(KeyCode.UpArrow))
    {
        rigidbody.MovePosition(transform.position + Vector3.up* movePower * Time.deltaTime);
    }

}
1

1 Answers

1
votes

The simplest way is to remove the Rigidbody from all cubes as they are supposed to be fixed. Another way is to mark the cubes' Rigidbody components as Kinematic, but this meant to be used for moving objects like player characters that should participate in physics but should not be moved by the engine.

I recommend reading the Unity Physics man page.


Update:

More things to consider:

  • Don't check Is Trigger
  • Check that your Layer Collision Matrix is set up right (menu Edit/Project Settings/Physics)
  • If the velocity is pretty high, physics engine might get confused and collision are not detected
  • Ensure that the models are not scaled down or up extremely (best is to have scale = 1)
  • The best mass for Rigidbody.mass is 1
  • Be careful when playing with PhysicsManager settings like Min Penetration For Penalty or Solver Iteration Count
  • Use gravity if possible
  • Never move by manipulating Transform directly. Use Rigidbody methods instead
  • Avoid calling rigidbody.MovePosition on every update if it's a constant linear motion. Do it once and leave it untouched
  • Remember to use FixedUpdate for calling rigidbody.MovePosition etc.