- Home /
turn 180 spaceship
Hello UA
Excuse my English, I tell them what my problem.
I'm programming the physics for a spacecraft, I've managed to turn like an airplane, when you move to the sides, and am now developing the 180 degree turn.
I succeeded, but my problem is that this rotation depends on the time so I have developed.
He commented that the turn has first to turn on the X axis to show the bottom of the spacecraft, and then rotate in the Z axis to return to the right side.
This shift should be discontinued at any time and start again, in the sense that is given to ASWD.
I would not be so, as you should be able to change the sense of direction at the moment is making the turn. I spent a bit of code, clarified that the move to the sense in watching the ship, already developed elsewhere.
 turnSpeed = 7.0f;
 tiltSpeed = 3.0f;
 tiltAngle = 40.0f;
 angle = 0.0f;
 dir = 1.0f;
 terminoGiro = true;
     
 parte1 = false;
 parte2 = false;
 
 duracionGiro = new LinearInterpolator(0,1,1);
 void Girando()
 {
     horizontalDirectionTurn = Input.GetAxis("Horizontal");
     verticalDirectionTurn = Input.GetAxis("Vertical");
 
     dTime = Time.deltaTime;
     
     desiredDirection = new Vector3(horizontalDirectionTurn, 0, verticalDirectionTurn).normalized;
     
     if (terminoGiro)
     {
        if (!desiredDirection.Equals(Vector3.zero))
         {
             if ((Vector3.Angle(desiredDirection,transform.forward) > 2) && 
                      (Vector3.Angle(desiredDirection,transform.forward) < 100)) { 
                 
                 if (Vector3.Cross(desiredDirection, currentDirection).y > 0.0f)
                 {
                     dir = 1.0f;
                 }
                 else
                 {
                     dir = -1.0f;
                 }
                 angle += tiltSpeed * dTime;
                 currentDirection = 
                 Vector3.Slerp(currentDirection, desiredDirection, turnSpeed * dTime);
                 
                 angle = Mathf.Clamp(angle, 0.0f, 1.0f);
                 transform.LookAt(transform.position + currentDirection);
              
                 body.localEulerAngles = new Vector3(0,0, tiltAngle * angle * dir);
             } 
             else if (Vector3.Angle(desiredDirection,transform.forward) > 100) 
             {
                 terminoGiro = false;
                 parte1 = true;
             }
             else
             {
                 angle -= tiltSpeed * dTime;
                 angle = Mathf.Clamp(angle, 0.0f, 1.0f);
                 transform.LookAt(transform.position + currentDirection);
              
                 body.localEulerAngles = new Vector3(0,0, tiltAngle * angle * dir);
             }    
         }
         else
         {
             angle = 0;
             angle = Mathf.Clamp(angle, 0.0f, 1.0f);
             transform.LookAt(transform.position + currentDirection);
              
             body.localEulerAngles = new Vector3(0,0, tiltAngle * angle * dir);
         }
     }
     else
     {
         if(parte1)
         {
             currentDirection = 
             Vector3.Slerp(currentDirection, desiredDirection, turnSpeed * dTime);
             transform.Rotate(-180 * dTime,0,0,Space.Self);
             
             duracionGiro.Interpolate(); //timer 1s
             
             if (duracionGiro.HasFinished()) //timer finish?
             {
                 duracionGiro.Reset();
                 parte2 = true;
                 parte1 = false;
             }
             
         }
         if (parte2)
         {
             currentDirection = 
             Vector3.Slerp(currentDirection, desiredDirection, turnSpeed * dTime);
             
             transform.Rotate(0,0,180 * dTime,Space.Self);
             
             duracionGiro.Interpolate();
             
             if(duracionGiro.HasFinished())
             {
                 duracionGiro.Reset();
                 parte2 = false;
                 terminoGiro = true;
                 transform.LookAt(transform.position + currentDirection);
             }
         }
     }
 }
From already thank you very much
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                