Advertisement

Pause state for every half rotation of a rotating object?

Started by September 11, 2016 09:11 AM
2 comments, last by FirstStep 8 years, 2 months ago

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?

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!

Advertisement

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

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

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

This topic is closed to new replies.

Advertisement