Home > Blockchain >  Unity OnMouseDrag triggered but all variations of Input.OnMouseButtonDown return false
Unity OnMouseDrag triggered but all variations of Input.OnMouseButtonDown return false

Time:08-24

I have a simple 3d object in my scene that I attached a script onto so I can rotate it freely in 3D space using mouse, the script attached is shown below:

float rotationSpeed = 6;

    private void onm ouseDrag()
    {
        Debug.Log(Input.GetMouseButtonDown(0)   " 0");
        Debug.Log(Input.GetMouseButtonDown(1)   " 1");
        Debug.Log(Input.GetMouseButtonDown(2)   " 2");
        if(Input.GetMouseButtonDown(0))
        {
            updatePrimitiveRotation();
        }
    }

    private Vector2 getDragAmount()
    {
        Vector2 dragAmount = new Vector2(Input.GetAxis("Mouse X") * rotationSpeed,
                                         Input.GetAxis("Mouse Y") * rotationSpeed);

        return dragAmount;
    }

    private void updatePrimitiveRotation()
    {
        Vector2 drag = getDragAmount();

        gameObject.transform.Rotate(Vector3.down, drag.x, Space.World);
        gameObject.transform.Rotate(Vector3.right, drag.y, Space.World);
    }

For some reason, the onm ouseDrag is always triggered as needed, but the shape never rotated! When I added the 3 debug lines they all result in false.. I was wondering if it was because I was using the buttons in my trackpad instead of a normal mouse, but searched into that and gathered that both work same, so I am not sure what the issue is here. Another issue I just found is that my right mouse button never triggers the onm ouseDrag, it's only the left button (still all 3 debug lines output "false")

CodePudding user response:

GetMouseButtonDown only returns true for the frame the button changed from released to pressed. GetMouseButton should do the trick.

  • Related