Search Unity

Question Ball Falling Through Rotating Rigidbody

Discussion in 'Physics' started by larynachos, Jun 28, 2020.

  1. larynachos

    larynachos

    Joined:
    Aug 6, 2012
    Posts:
    8
    Hello! I'm making a maze puzzle game, where you click and drag a maze box to rotate it. Inside is a little ball, and you're supposed to tilt the box to maneuver the ball into a hole in the center. My problem is that moving the box too quickly will result in the ball clipping through the ground. Here is my current script:

    Code (CSharp):
    1.     public float horizontalSpeed = 2.0F;
    2.     public float verticalSpeed = 2.0F;
    3.     void OnMouseDrag()
    4.     {
    5.         float h = horizontalSpeed * Input.GetAxis("Mouse X");
    6.         float v = verticalSpeed * Input.GetAxis("Mouse Y");
    7.         transform.Rotate(v, h, 0);
    8.     }
    I realized after a bit of googling that I'm supposed to be using torque instead, but I'm not sure how to go about that. I tried subbing this snippet from a different thread for transform.Rotate instead but nothing happened:

    Code (CSharp):
    1.     rb.AddTorque(Vector3.up * torque * -Input.GetAxis("Mouse X"));
    2.  
    3.     rb.AddTorque(Vector3.right * torque * Input.GetAxis("Mouse Y"));
    The box is lying flat, with the camera positioned above it looking straight down. Can anyone help me? Thanks a ton!
     
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    I suspect you checked the “kinematic” flag on your maze rigidbody. Use rigidbody.MoveRotation instead of torque as this will only work for rigidbodies that react to torques and forces.

    But if the gameplay permits it, I would rather make the maze static (no rigidbody component) and just tilt the camera so it looks like the maze is tilted and move the ball with AddForce