Search Unity

Landing Gear Function

Discussion in 'Scripting' started by Wolfsshadow, Aug 2, 2015.

  1. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    Good Morning All,

    I've been going through the standard assets and trying to adjust/add different features to them. One of the features I wanted to change was the ability to manually raise and lower the landing gear. I have reached a script that no longer has any errors, but cannot seem to get the gear to function using the "L" key. It now just stays down, and my guess is it has something to do with the if statements I have for determining the state of the gear. Any help would be most appreciate!

    using System;
    using UnityEngine;
    using UnityStandardAssets.CrossPlatformInput;

    namespace UnityStandardAssets.Vehicles.Aeroplane
    {
    public class LandingGear : MonoBehaviour
    {
    public UnityEngine.UI.Text altLabel;

    private enum GearState
    {
    Raised = -1,
    Lowered = 1
    }

    // The landing gear can be raised and lowered at differing altitudes.
    // The gear is only lowered when descending, and only raised when climbing.

    // this script detects the raise/lower condition and sets a parameter on
    // the animator to actually play the animation to raise or lower the gear.

    public float raiseAtAltitude = 40;
    public float lowerAtAltitude = 40;

    private GearState m_State = GearState.Lowered;
    private Animator m_Animator;
    private Rigidbody m_Rigidbody;
    private AeroplaneController m_Plane;

    // Use this for initialization
    private void Start()
    {
    m_Plane = GetComponent<AeroplaneController>();
    m_Animator = GetComponent<Animator>();
    m_Rigidbody = GetComponent<Rigidbody>();
    }


    // Update is called once per frame

    private void Update()
    {
    float alt = m_Plane.Altitude;
    altLabel.text = alt.ToString("Alt: 000ft");

    if (m_State == GearState.Lowered)
    {
    if (CrossPlatformInputManager.GetButton("l"))
    {
    m_State = GearState.Raised;
    }
    }
    if (m_State == GearState.Raised)
    {
    if (CrossPlatformInputManager.GetButton("l"))
    {
    m_State = GearState.Lowered;
    }
    }

    // set the parameter on the animator controller to trigger the appropriate animation
    m_Animator.SetInteger("GearState", (int) m_State);

    }
    }
    }
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    [code ][/code ] tags please.

    Try to keep in mind that changes you make are immediate. You're checking a state, then if it's true you're changing it. You're then (a moment later) checking for the second state, and if it's true, then changing it. Your script is looping through changing the state twice in rapid succession.

    Change this:
    Code (csharp):
    1. if (m_State == GearState.Raised)
    to this:
    Code (csharp):
    1. else if (m_State == GearState.Raised)
    or to "else" and nothing else, so that if the first state check was successful, the second will be skipped.

    Also keep in mind that "GetButton" typically means "While Button is Pressed" and not "If Button Was JUST Pressed" (though I'm not familiar with that input manager). That being the case, and keeping in mind that this function runs several dozen times a second, you need to either change the GetButton to GetButtonDown (if such a function exists, it'll only be "true" for a single frame as the button is pressed, and won't be "true" again until the button is released and pressed again) or create a timer so that the landing gear can't be changed more often than once every few seconds or something.
     
    Last edited: Aug 2, 2015
  3. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    You're awesome! Thanks for the reply! I will go try that out now!
     
  4. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    That's awesome:) I just added "else" and it worked. I will start playing around with the GetButton portion as hitting "L" doesn't always work the first time, but thank you very much for the help!
     
  5. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    Sorry for so many responses, GetButtonDown now works every time it is pressed. Thank you for the thorough explanation that solved a problem that I didn't even have until you solved my first problem.