return float value for mouse input +/-Y

I am trying to return a value between 0 and 1 whenever user is clicking on holding the left mouse button over a collision object called "RightPull". This is what i have so far.

    var RPullValue : float = 0.0;

    function Update()
    {
         if (Input.GetMouseButtonDown(0))
         {
        var hit : RaycastHit;
        var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);      
        if (Physics.Raycast(ray, hit)){
             if (hit.collider.name == "RightPull"){
                  hit.rigidbody.AddForce (-hit.normal * 100);
                  RPullValue += Input.GetAxis("Mouse Y") *0.01;
             }
         }
    }
}

This works to an extent. The extent is that it only registers a value increase for one moment when the user clicks, but not when you drag the mouse. Other issues are that the value can increase or decrease beyond 0 and 1. Any ideas how I can get this value to adjust more smoothly and interactively?

2 Answers

2

I finally solved this problem by attaching a script directly to the collider game object, not to the general scene script. The object script reads like this:

function OnMouseDrag () {
            Debug.Log(PullValue);
            PullValue += Input.GetAxis("Mouse Y") * 0.5;
            PullValue = Mathf.Clamp(PullValue, 0, 1);
}

Your Input.GetAxis("Mouse Y") is inside the if statement if (Input.GetMouseButtonDown(0)). So the mouse input will only be calculated when the user presses the mouse button.

From the reference Input.GetAxis:

“If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1…1.”

Make sure that Mouse X and Mouse Y are mapped to mouse movement in the Input Manager.

I can use a clamp value to limit the value between 0 and 1, but this answer still does not address how I can limit the mouse input to only be effected when the mouse button is held over the collider.