x


Mouse Problem

I've go the following script which rotates an object in front of me as I mouse over it. However, when I lift up on the mouse button, the rotation stops. I would like the function to continue, even when I lift up. How can I do this? Here's the code:

var speed : int;
var friction : float;
var lerpSpeed : float;
private var xDeg : float;
private var yDeg : float;
private var fromRotation : Quaternion;
private var toRotation : Quaternion;

function Update () {
    if(Input.GetMouseButton(0)) {
        xDeg -= Input.GetAxis("Mouse X") * speed * friction;
        yDeg -= Input.GetAxis("Mouse Y") * speed * friction;
        fromRotation = transform.rotation;
        toRotation = Quaternion.Euler(yDeg,xDeg,0);
        transform.rotation = Quaternion.Lerp(fromRotation,toRotation,
                                  Time.deltaTime  * lerpSpeed);
    }
}
more ▼

asked Jun 23 '11 at 04:44 PM

AnimAlu gravatar image

AnimAlu
1 9 10 12

Please remember to format your code (the 10101001 button) :D

Jun 23 '11 at 05:03 PM Chris D

Thank you Chris! I'm new here :)

Jun 23 '11 at 05:04 PM AnimAlu
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

add in a boolean value to see if you've already clicked on it or not. Something like:

var activelyRot : Boolean = false;

function Update () {
    if(Input.GetMouseButton(0)) 
        activelyRot = !activelyRot;

    if(activelyRot){
        xDeg -= Input.GetAxis("Mouse X") * speed * friction;
        yDeg -= Input.GetAxis("Mouse Y") * speed * friction;
        fromRotation = transform.rotation;
        toRotation = Quaternion.Euler(yDeg,xDeg,0);
        transform.rotation = Quaternion.Lerp(fromRotation,toRotation,
                                  Time.deltaTime  * lerpSpeed);
    }
}

This should let you click once to begin controlling it, then click again to stop it.

more ▼

answered Jun 23 '11 at 05:07 PM

Chris D gravatar image

Chris D
2.5k 5 7 25

(comments are locked)
10|3000 characters needed characters left

Not sure why, but I'm getting a compiling error when I run this. complete code:

var speed : int;
var friction : float;
var lerpSpeed : float;
private var xDeg : float;
private var yDeg : float;
private var fromRotation : Quaternion;
private var toRotation : Quaternion;
var activelyRot : Boolean = false;

function Update () {
    if(Input.GetMouseButton(0)) 
        activelyRot = !activelyRot;

    if(activelyRot){
        xDeg -= Input.GetAxis("Mouse X") * speed * friction;
        yDeg -= Input.GetAxis("Mouse Y") * speed * friction;
        fromRotation = transform.rotation;
        toRotation = Quaternion.Euler(yDeg,xDeg,0);
        transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime  * lerpSpeed);
    }
}
more ▼

answered Jun 23 '11 at 05:32 PM

AnimAlu gravatar image

AnimAlu
1 9 10 12

In the future, please try to keep clarifications or feedback attached to the entry you're referring to. You can't comment on my answer yet (a quirk of how the rep system works here) so, in the mean time, just put comments on your question or edit your post.The answer area should be reserved for solutions.

In regards to the compile error, I apologize; I'm not somewhere I can test the code at the moment. What error are you receiving?

Jun 23 '11 at 05:42 PM Chris D
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x983
x723
x160
x15

asked: Jun 23 '11 at 04:44 PM

Seen: 811 times

Last Updated: Jun 23 '11 at 05:42 PM