0
\$\begingroup\$

I am new to Unity. I imported an OBJ model and assigned materials to it.

I want to rotate it using the mouse in the game, using this script:

using UnityEngine;

public class MouseDragRotate : MonoBehaviour {
    float rotationSpeed = 0.2f;

    void OnMouseDrag()
    {
        float XaxisRotation = Input.GetAxis("Mouse X")*rotationSpeed;
        float YaxisRotation = Input.GetAxis("Mouse Y")*rotationSpeed;
        // select the axis by which you want to rotate the GameObject
        transform.RotateAround (Vector3.down, XaxisRotation);
        transform.RotateAround (Vector3.right, YaxisRotation);
    }
}

I tried to apply this rotation script to the OBJ asset in my Project folder, but I wasn't able to find an Add Component option in its Inspector.

So I dragged it to the Hierarchy panel to create an instance of the object in my scene. I applied my materials again to this instance.

Then I added the rotation script using the Add Component button in the Inspector.

But when running the game in play mode, I can't rotate it.

I verified that the script above works perfectly for the default Cube or Plane primitives in Unity, but not for this imported object.

How can I make this script work with my OBJ model?

\$\endgroup\$
  • 1
    \$\begingroup\$ It sounds like you probably added your rotation component to the wrong object in your hierarchy - say an empty child object instead of the parent of the mesh. Or you could be using a material that overrides the object's transformation matrix. Unfortunately, knowing only that the object was from an OBJ file, and that you applied some unspecified materials to it, doesn't give us enough information to reproduce this problem in a new project. Can you walk us through the steps we need to follow to see the same error you're seeing? \$\endgroup\$ – DMGregory Jun 20 at 11:36
  • 1
    \$\begingroup\$ Is it safe to assume your object has a Collider to detect when the mouse is clicking / dragging over it, so Unity knows to call OnMouseDrag on that object's scripts? \$\endgroup\$ – DMGregory Jun 20 at 11:38
  • \$\begingroup\$ @DMGregory I added the box Collider manually now it working .. Thanks \$\endgroup\$ – Lord Commander Jun 20 at 11:50
  • \$\begingroup\$ please add a detailed answer about box collider and its working principle and uses \$\endgroup\$ – Lord Commander Jun 20 at 11:51
  • \$\begingroup\$ I will accept and upvote and others user will get benefited \$\endgroup\$ – Lord Commander Jun 20 at 11:51
2
\$\begingroup\$

Unity's OnMouseDown / OnMouseDrag / etc MonoBehaviour messages only work when the object has a collider attached.

This is because when you use these methods, under the hood, the engine fires a physics raycast each frame from the main camera, through the position of the mouse, to find out what object is under it, so it knows what object to fire these messages on.

Only colliders will be hit by that raycast — it ignores rendered meshes and such (otherwise the performance cost could get extreme: physics colliders are usually much simpler than detailed display geometry). This also gives you the ability to control which objects should block that raycast — like a solid wall — versus not — like a puff of smoke particles or a translucent glass window.

\$\endgroup\$
  • \$\begingroup\$ I added the box Collider manually now it working .. Thanks – \$\endgroup\$ – Lord Commander Jun 20 at 12:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.