- Home /
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);
}
}
Please remember to format your code (the 10101001 button) :D
Answer by Chris D · Jun 23, 2011 at 05:07 PM
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.
Answer by AnimAlu · Jun 23, 2011 at 05:32 PM
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);
}
}
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?
Your answer
![](https://koobas.hobune.stream/wayback/20220612115614im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate to mouse seems to be inversed 1 Answer
Rotate the player to face where the mouse is pointing 3 Answers
Rotation Question 2 Answers
Camera Rotate to mouse position -2 Answers