Jump to content
  • Advertisement
Sign in to follow this  
FirstStep

Pause state for every half rotation of a rotating object?

This topic is 826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone help me how to pause a rotating object every time it reach half of the whole rotation?

 

I have this code but I can only get the 1st half

 

 

  if(!pause){
     transform.Rotate(new Vector3(0,0,-1)* rotationspeed * Time.deltaTime);
     pauseAngle = transform.rotation.eulerAngles.z;
    }
    if(pauseAngle <= 180 && timerPause > 0 ){
     timerPause -= Time.deltaTime;
     pause = true;
    
    }
     if(timerPause <= 0){
      timerPause = 2;
      pause = false;
     }

Anyone?

Share this post


Link to post
Share on other sites
Advertisement

The first thing to always do, is to understand why it does not work as you think it should.

To do this, you have to sort of 'watch' the code run. There are two ways to do that.

 

The first option is to use a debugger. Set a breakpoint at the first 'if'. Every time the processor runs this code, the debugger stops at that point, and you can examine the variables, execute a single statement at a time, step by step. Check that the code does the thing you expect it to do.

 

The second option is to use what is called "printf debugging". Basically, you print a few lines of output what happens in these lines of code every time it is executed. For example, print the value of "pauseAngle" and "timerPause" just before the second 'if' line. I assume you mean to trigger that with the second half rotation, so something must be wrong in that condition. By printing the values each time this code is executed, you can see whether the variables take the right value.

 

Good luck huinting the bug!

Share this post


Link to post
Share on other sites

Can someone help me how to pause a rotating object every time it reach half of the whole rotation?

 

If I understand you correctly then the 3 steps below would get you to half of the whole rotation and you would simply need to adjust your code a bit

 

1. To get half of a unit-step rotation:  You just need reduce the unit-step rotation by half

 

2. To find combined of the whole rotation in multiples of the unit-step rotation, you would have to calculate the total angle, find how many unit-steps gets you there and then find half the total number of unit-steps to get there

    

3. Finally you combine steps 1 & 2

    Ex. if the original full rotation is 39 degrees and you need to rotate an object through to 19.5 degrees, but each unit-step rotation is 3 degrees 

    step 1 takes you to 18 degrees and step 2 takes you to 19.5 degrees

Share this post


Link to post
Share on other sites

okay I think I found a solution but full of hacks and even I am not comfortable but it works

 

 

 transform.Rotate(new Vector3(0,0,-1)* rotationspeed * Time.deltaTime);
     pauseAngle = transform.rotation.eulerAngles.z;
     
    
     if(!pause){
      timerPause += Time.deltaTime;
      
      time = (distance / rotationspeed) - 0.2f; // modified formula of speed = distance / time
        if(timerPause >= time){
         print(timerPause + " " + time );
         print( " pause angle " +( distance - pauseAngle - 20));
         
         distance = 180 + 20;
        
         float ss =rotationspeed * timerPause;
         //print(ss);
         
          //print("pause setting to true");
          rotationspeed = 0;
          transform.eulerAngles = new Vector3(transform.eulerAngles.x,transform.eulerAngles.y,distance - 20);
          
          pauseTimerOffset = 2f;
          
          pause = true;
        }
     }
     if(pause){
      pauseTimerOffset -= Time.deltaTime;
      if(pauseTimerOffset <= 0){
       //print("pause is donne setting to false");
       pause = false;
       timerPause = 0;
       rotationspeed = 100f;
       
      }
     }

So the formula to get the time is from the speed = distance/time but modify it to find the time. I then subtract -0.2f on the condition checking, after that I added 20 to the distance which is 180 because every time it pause the angle is off for about 20 units. and then I readjust the rotation but this time subtract the 20 units.

 

 Code is full of hack but if anyone found a better solution feel free to post it here :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!