0
\$\begingroup\$

I am making a game (Unity) in which the player controls a blind person with a stick. The stick is controlled by the mouse using the script

void Update()
{
    Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
}

The stick must collide with certain obstacles and not pass through them. What I have done so far is create a 3D RigidBody on the stick, and 3D Box Colliders on both the obstacle and the stick. The stick is hinged with a 3D hinge joint to the player's hand (circled in red). However, the collision does not seem to be working. The stick passes through the obstacle. However, it does jiggle about when it is inside the obstacle and sometimes gets detached from the hinge point.

enter image description here

Is there any reliable way of achieving this in Unity?

\$\endgroup\$
  • 2
    \$\begingroup\$ This is a common mistake: moving an object with its Transform and expecting the physics engine to somehow intercept that transform change and modify it before it's applied to prevent collider penetration. It doesn't work that way: the physics engine doesn't get a chance to act here until after the transform has already teleported the object into an intersecting position. Instead, you want to use Rigidbody members like angularVelocity or AddTorque to apply the rotation within the physics engine, so it can integrate the motion physically and handle collisions accordingly. \$\endgroup\$ – DMGregory May 5 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.