image rotation

Hi folks,

I’m looking to accomplish the following with the attached script. I have been at it on and off for a day now and can’t seem to get it right.

What I would like to accomplish is the following:

when Fire1 is press, an img would rotate until the Fire1 is pressed again, which will stop the rotation and reset the img rotation back to 0,0,0.

any help would be greatly appreciated.

Thanks

2055663–133759–SpinOnClick.cs (1.03 KB)

The problem is that FixedUpdate is updating too quickly to detect individual presses. On each press, you’re flipping _rotateStatus between true and false several times before you finally release the button, causing the flickering behavior. What you want to do is define some time threshold between valid presses. Look at this example. Also, keep in mind that you shouldn’t use FixedUpdate for input – it can trigger multiple times in the same frame, and it can also completely miss input.

galf,

I tried the Time.time example you linked me to. The effect of that work; however, It only rotates the image when I press Fire1. It doesn’t continuously rotate when Fire1 is press and wait until I press Fire1 again.

I think I might need to do a do…while loop instead of an IF statement … or I can just make the image into an animation and call the animation when Fire1 is click.

Something like this might work better.

void FixedUpdate () {

        if (Input.GetButtonDown("Fire1") )
        {

            if(_rotateStatus == false){

                Debug.Log("if false, set to true and rotate: " + _rotateStatus);
                _rotateStatus = true;
                rotateMe(_rotateStatus);

            }

            else if(_rotateStatus == true){

                Debug.Log("if false, set to true and rotate: " + _rotateStatus);
                _rotateStatus = false;
                rotateMe(_rotateStatus);
            }


        
        }
    
    }

Sorry, I got your example working last night, but I wanted you to work through the details a bit. At a high level, you need to enter and exit from a “rotation state”. So, when you detect that first press, set the state (a boolean) to true. When you receive another press, which is after your delay, toggle it back to false. And then outside of that conditional, you’ll want to, every frame, rotate your object if it’s in that rotation state, or reset its rotation if its outside that state.

sorry for the late response, had been a busy week at work. I had to create a new method, isGetButton(), to achieve the result and it took me a while to figure out where to put the delay and get it work properly.

2060591–134214–SpinOnClick.cs (1.02 KB)