[SOLVED] How do I make these aircraft wings snap back to their original position?

Here is the code in C#:

The problem line is:


if (Idle)

{
air_pressure.active=false;

front_fins.transform.Rotate(0, 0, 0);
rear_fins.transform.Rotate(0, 0, 0);

***This is where I try to set the wings to their original position which is exactly 0,0,0

}


//FA-37 TALON JET CONTROLLER
//SEE FA-37 WEAPON CONTROLLER SCRIPT FOR THE WEAPONS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TalonControl : MonoBehaviour {
public GameObject talon;
public GameObject thrust;
public GameObject engine;
public GameObject vtol_jet;
public GameObject air_pressure;
public GameObject cockpitcam;
public GameObject rearcam;
public GameObject cinecam;
public GameObject front_fins;
public GameObject rear_fins;
public GameObject rear_rudder1;
public GameObject rear_rudder2;
public GameObject gears;
public bool PitchUp = false;
public bool PitchDown = false;
public bool TurnLeft = false;
public bool TurnRight = false;
public bool RollLeft = false;
public bool RollRight = false;
public bool FlyUp = false;
public bool FlyDown = false;
public bool Idle = false;
public float vtol = 0;

    // Use this for initialization
    void Start () {
    
    }
 
    // Update is called once per frame
    void Update () {
    
     if (vtol<=0){
     talon.transform.Translate(Vector3.forward * 20 * Time.deltaTime);
     vtol_jet.active=false;
     thrust.active=true;
     gears.active=false;
    }
    if (vtol>=1){
     talon.transform.Translate(Vector3.forward * 0 * Time.deltaTime);
     vtol_jet.active=true;
     thrust.active=false;
     gears.active=true;
 
    }
        if (FlyUp)
         {
                 talon.transform.Translate(Vector3.up * 5 * Time.deltaTime);

        
         
         }
    
    
        if (FlyDown)
         {
             talon.transform.Translate(Vector3.down * 5 * Time.deltaTime);

        
         
         }
          if (PitchUp)
         
             {
            
                talon.transform.Rotate(Vector3.left, 20 * Time.deltaTime);
                front_fins.transform.Rotate(Vector3.left, 100 * Time.deltaTime);
                rear_fins.transform.Rotate(Vector3.right, 100 * Time.deltaTime);
                air_pressure.active=true;
            
             }
         if (PitchDown)
         
             {
                talon.transform.Rotate(Vector3.right, 20 * Time.deltaTime);
                front_fins.transform.Rotate(Vector3.right, 100 * Time.deltaTime);
                rear_fins.transform.Rotate(Vector3.left, 100 * Time.deltaTime);
                air_pressure.active=true;
            
             }
         
         if (TurnLeft)
         
             {
                talon.transform.Rotate(Vector3.down, 20 * Time.deltaTime); 
             }
         
             if (TurnRight)
         
             {
                talon.transform.Rotate(Vector3.up, 20 * Time.deltaTime); 
             }
         
             if (RollLeft)
         
             {
                talon.transform.Rotate(Vector3.forward, 20 * Time.deltaTime); 
             }
         
             if (RollRight)
         
             {
                talon.transform.Rotate(-Vector3.forward, 20 * Time.deltaTime); 
             }
         
           // PROBLEM
             if (Idle)
        
             {
                air_pressure.active=false;
            
                front_fins.transform.Rotate(0, 0, 0);
                rear_fins.transform.Rotate(0, 0, 0);
            
            
            
             }
           //PROBLEM
         
         
         
    }
 
 
 
    public void Vtol1()
     {
         vtol=1;
            
     }
    public void Vtol0()
     {
         vtol=0;
            
     }
 
    public void Cockpit()
     {
         cockpitcam.active=true;
         rearcam.active=false;
         cinecam.active=false;
         engine.active=false;
            
     }
    public void Rear()
     {
         cockpitcam.active=false;
         rearcam.active=true;
         cinecam.active=false;
         engine.active=true;
     
            
     }
 
    public void Cine()
     {
         cockpitcam.active=false;
         rearcam.active=false;
         cinecam.active=true;
         engine.active=true;
          FlyUp=false;
           FlyDown=false;
            
     }
 
 
 
 
 
 
 
 
 
    public void onPitchUp()
     {
         PitchUp=true;
          PitchDown=false;
          TurnLeft=false;
          TurnRight=false;
                RollLeft=false;
           RollRight=false;
            FlyUp=false;
           FlyDown=false;
           Idle=false;
     }
 
     public void onPitchDown()
     {
         PitchUp=false;
          PitchDown=true;
          TurnLeft=false;
          TurnRight=false;
                RollLeft=false;
           RollRight=false;
            FlyUp=false;
           FlyDown=false;
           Idle=false;
     }
 
     public void onTurnLeft()
     {
         PitchUp=false;
          PitchDown=false;
          TurnLeft=true;
          TurnRight=false;
                RollLeft=false;
           RollRight=false;
            FlyUp=false;
           FlyDown=false;
           Idle=false;
     }
 
 
     public void onTurnRight()
     {
         PitchUp=false;
          PitchDown=false;
          TurnLeft=false;
          TurnRight=true;
                RollLeft=false;
           RollRight=false;
            FlyUp=false;
           FlyDown=false;
           Idle=false;
     }
     public void onIdle()
     {
         PitchUp=false;
          PitchDown=false;
          TurnLeft=false;
          TurnRight=false;
           RollLeft=false;
           RollRight=false;
            FlyUp=false;
           FlyDown=false;
            Idle=true;
            
     }
    public void onRollLeft()
     {
         PitchUp=false;
          PitchDown=false;
          TurnLeft=false;
          TurnRight=false;
          RollLeft=true;
          RollRight=false;
           FlyUp=false;
           FlyDown=false;
           Idle=false;
            
     }
     public void onRollRight()
     {
         PitchUp=false;
          PitchDown=false;
          TurnLeft=false;
          TurnRight=false;
          RollLeft=false;
          RollRight=true;
           FlyUp=false;
           FlyDown=false;
           Idle=false;
            
     }
      public void onFlyUp()
     {
         PitchUp=false;
          PitchDown=false;
          TurnLeft=false;
          TurnRight=false;
           RollLeft=false;
           RollRight=false;
           FlyUp=true;
           FlyDown=false;
           Idle=false;
            
     }
      public void onFlyDown()
     {
         PitchUp=false;
          PitchDown=false;
          TurnLeft=false;
          TurnRight=false;
           RollLeft=false;
           RollRight=false;
           FlyUp=false;
           FlyDown=true;
           Idle=false;
            
     }
}

try this:

front_fins.transform.rotation = Quaternion.identity;
//use localRotation , maybe, instead.

Pretty sure Rotate is “modify rotation by…”, and you’re code is saying “hey, modify it by zero,zero,zero”.

An EulerAngle should make it snap back to 0,0,0

front_fins.transform.eulerAngles = new Vector3(0, 0, 0);

Unfortunately it is still not working. It just snaps to different angles. No video available at the moment but it just behaves like that in the video but now it snaps back at different rotation angles like -70 -45

Did you try localRotation?

I tried but not much. I will see when I have free time

try my code with localRotation. Unless something else is moving them, it should work (if they are children, and you want them to have a … local rotation of 0,0,0 (eulers)).

Alright, I gave up controlling the fins and flaps via script. I just animated them with the built-in unity animator. I have made separate animator clips for each wing.

Only issue left now is that the wings do not transition smoothly as you can see in the video demo.

You never wrote back about the rotation from before :slight_smile: Which I may have updated to say you could use rotate towards.

Anyhow, so when you’re not climbing or descending , you want them to animate back to flat?

If you’re on animations, can you just do that? Animate back to no rotation?

Yeah everything I need is in the animation. I fixed it by reversing each animation when the wings snaps back. Problem solved. Animation is easier than controlling each by script. I think that this is what these flight sims do, they animate each plane and then just play each animated clip when the player presses a button.

Cool, glad you got it working.

I’ve just done this myself. Here’s what I came up with

    //Evlevator
    public Transform Elevator;
    [Range(0f, 90f)]
    public float MaxRotation = 50f;
    public float RotationSpeed = 2f;
    public float StabilizationSpeed = 2f;

    public void Update()
    {
        ElevatorControl();
    }

    private void ElevatorControl()
    {
        //Set Rotation
        Elevator.localRotation = Quaternion.Lerp(Elevator.localRotation,
            Quaternion.Euler(Elevator.localRotation.eulerAngles.x, Elevator.localRotation.eulerAngles.y, -MaxRotation * Input.GetAxis("Vertical")),
            Time.deltaTime * RotationSpeed);
        //Return to Rest
        Elevator.localRotation = Quaternion.Lerp(Elevator.localRotation,
            Quaternion.Euler(Elevator.localRotation.x, Elevator.rotation.y, Elevator.localRotation.eulerAngles.z),
            Time.deltaTime * StabilizationSpeed);
    }

This is the code in action;

On a mission to try to fix bad lerp and slerp, I offer the suggestion that you could rotate towards or smoothdamp instead. lol
Sorry, I also wanted to say “Cool code”. :slight_smile: