Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

image rotation

Discussion in 'Scripting' started by Red8Rain, Apr 6, 2015.

  1. Red8Rain

    Red8Rain

    Joined:
    Jan 3, 2014
    Posts:
    7
    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
     

    Attached Files:

  2. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    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.
     
  3. Red8Rain

    Red8Rain

    Joined:
    Jan 3, 2014
    Posts:
    7
    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.
     
  4. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Something like this might work better.


    Code (CSharp):
    1. void FixedUpdate () {
    2.  
    3.         if (Input.GetButtonDown("Fire1") )
    4.         {
    5.  
    6.             if(_rotateStatus == false){
    7.  
    8.                 Debug.Log("if false, set to true and rotate: " + _rotateStatus);
    9.                 _rotateStatus = true;
    10.                 rotateMe(_rotateStatus);
    11.  
    12.             }
    13.  
    14.             else if(_rotateStatus == true){
    15.  
    16.                 Debug.Log("if false, set to true and rotate: " + _rotateStatus);
    17.                 _rotateStatus = false;
    18.                 rotateMe(_rotateStatus);
    19.             }
    20.  
    21.  
    22.        
    23.         }
    24.    
    25.     }
     
  5. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    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.
     
  6. Red8Rain

    Red8Rain

    Joined:
    Jan 3, 2014
    Posts:
    7
    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.
     

    Attached Files: