- Home /
How to rotate object on mouse click?
I have a project where when I click I set the gameObjects Z position to look up(0.09) and then if its not being clicked I make it wait 2 seconds and then set its position to look forward(0) again but for some reason everything I have tried doesn't work. It only works on the first click and once 2 seconds have passed sets it back to zero but will not set it to look up on any of the following clicks? Can someone please show me how they would do it in javascript if possible please?
#pragma strict
var rotationUp = 0.0f;
var rotationDown = 0.0f;
var rotationDownDelay = 0.0f;
var jumpSound : AudioSource;
function Update () {
if (Input.GetButtonDown("Fire1")) {
rigidbody2D.velocity = Vector2(0.0f, 7.0f);
jumpSound.Play();
gameObject.transform.rotation.z = rotationUp;
LookDown();
}
}
function LookDown () {
yield WaitForSeconds (2);
gameObject.transform.rotation.z = rotationDown;
}
You should post your script so someone can help. It's impossible to know where you went wrong without the script
Right now, on every update that the button is down, you will be starting another LookDown(), which will wait for 2 seconds. That might be where your problem lies. I think you just want LookDown to happen once after you press the button. So, I think it would be better to do the LookDown() on button up, therefore, you guarantee only one LookDown() instance per button press.
On top of that, I would make a bool system to check the state of the LookDown() to ensure that an instance is not still in progress before calling it again. If it is in progress, stop the current one and start a new instance.
On button up seems to do the same thing which is it waits 2 seconds on the first click but the following ones only use a fraction of that delay. And if you could give me an example of that boolean system it'd be greatly appreciated
Another problem (and perhaps the problem) is that you are directly assigning a single component of the Transform.rotation. The Transform.rotation is a Quaternion. You should not manipulate the components unless you understand Quaternions. You can use Transform.eulerAngles, but you need to assign all three angles. From the reference:
Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations. When setting them to a new value set them all at once
So on line 13, you can do:
transform.eulerAngles = Vector3(0.0,0.0,90.0);
Then on line 13 you can do:
transform.eulerAngles = Vector3(0.0,0.0,0.0);
Or you can do:
transform.rotation = Quaternion.identity;
Answer by ben-tmg · Mar 12, 2014 at 12:05 AM
Ok, I'll take a stab at it: (I haven't tested this)
I put in the suggestions from the other posters.
#pragma strict
var rotationUp = Vector3(0.0, 0.0, 90.0);
var rotationDown = Vector3(0.0, 0.0, 0.0);
var rotationDownDelay = 0.0f;
var isLooking = false;
var jumpSound : AudioSource;
function Update () {
if (Input.GetButtonUp("Fire1")) {
//I assume this isn't related to the rotation stuff
rigidbody2D.velocity = Vector2(0.0f, 7.0f);
jumpSound.Play();
if(isLooking == false)
{
isLooking = true;
gameObject.transform.rotation = rotationUp;
LookDown();
}
}
}
function LookDown () {
yield WaitForSeconds (2);
gameObject.transform.rotation = rotationDown;
isLooking = false;
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Z rotation locks to certain value randomly? 0 Answers
[SOLVED]Possible Alternation of Transform.Rotate 1 Answer
Click to destroy object 2 Answers