OnMouseOver object

Rev2

Thanks Speed & Tom shame I can’t say they were both answers, but I have used raycasting, and modified the code to:

var rotationSpeed = 10.0;
var lerpSpeed = 1.0;

private var speed = new Vector3();
private var avgSpeed = new Vector3();
private var dragging = false;
private var moveout = false;
private var targetSpeedX = new Vector3();

function OnMouseOver() 
{
    dragging = true;
}

function OnMouseOut() 
{
    moveout = true;
}

function Update () {

if (dragging)
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, 1000)) {
	speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
    avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
}
else {
    if (moveout) {
        speed = avgSpeed;
        dragging = false;
    }
    var i = Time.deltaTime * lerpSpeed;
    speed = Vector3.Lerp( speed, Vector3.zero, i);   
}


}
transform.Rotate(Vector3.up, speed.x * rotationSpeed, Space.World);
}

And it now works, thanks guys!!


Hi, so I have a script to rotate an object, and have the inertia continue to rotate once the mouse has moved off the object.

At the moment the script works when the mouse button is pressed and dragged over:

var rotationSpeed = 10.0;
var lerpSpeed = 1.0;

private var speed = new Vector3();
private var avgSpeed = new Vector3();
private var dragging = false;
private var targetSpeedX = new Vector3();

function OnMouseDown() 
{
    dragging = true;
}

function Update () 
{

    if (Input.GetMouseButton(0) && dragging) {
        speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
        avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    } else {
        if (dragging) {
            speed = avgSpeed;
            dragging = false;
        }
        var i = Time.deltaTime * lerpSpeed;
        speed = Vector3.Lerp( speed, Vector3.zero, i);   
    }

    transform.Rotate(Vector3.up, speed.x * rotationSpeed, Space.World);

}

However, I want to use OnMouseOver, so that when the mouse moves across an object it rotates on the axis, without the use of click.

Here is what I have tried so far:

I changed

function OnMouseDown() 
{
    dragging = true;
}

To

function OnMouseOver()
{
dragging = true;
}

Which doesn’t work, until the user clicks on it and drags, but after that it works when the mouse hovers over and moves across, but only in the direction in which the object was first spun.

So I would imagine the next place to look would be:

  if (Input.GetMouseButton(0) && dragging) {
        speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
        avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    } else {
        if (dragging) {
            speed = avgSpeed;
            dragging = false;
        }

Now I’m not sure what to change, at a guess its Input.GetMouseButton(0), and it would need to be something that is: while the mouse is over this object?

Sorry, I’m pretty new to Unity and coding, and I have feeling it’s going to be one of those simple things.

But any help and suggestions would be awesome, and much welcomed.

If you have multiple objects that will be using this, I would suggest using one script to utilize this. Here’s what I would do:

First create a new layer for these “rotating objects” and assign it to them. Put your script on a seperate object (not a rotating object). Use raycast (http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html) every update cycle to check if your mouse is hitting one of the rotating objects (using the layer of those objects as a layermask). If it’s hitting an object rotate its transform.

Input.GetMouseButton(0) is only true if you actually press the button. Try leaving that out and only test for “dragging”. Then don’t include the second part that checks for dragging again, if you need to do something if the mouse cursor left the object, then use a seperate boolean variable.