how to convert a toggle imput to vaule +/-

Hi! How can I make a button (toggle) in my case [Space] to percent, that means if I press 1, the thrust should decrease. and with 2 the thrust should increase. I’ve tried a lot, but I can only do it with one button. here is the code:

        if (Input.GetKeyDown(KeyCode.Space))
        {
            thrustPercent = thrustPercent > 0 ? 0 : 1f;
        }

If I understand you correctly, you can change it to a bool and use that in your thrust calculations instead.

bool thrusting;

void Update()
{
   if(Input.GetKeyDown(KeyCode.Space))
       thrusting = !thrusting; // this will flip the bool every time you distinctly press space
}

void FixedUpdate()
{
   if(thrusting)
       rigidbody.AddRelativeForce(0f, 0f, 5f); // or whatever
}

i can send you the whole code. I mean If I press spacebar, the plane accelerates. you only can switch between 0% and 100% thrust. but I’d lick to have 2 buttons 1 for increasing and 2 for decreasing.

You don’t need to send the whole code. So you want to have a “throttle up” button and a “throttle down” button that move the total thrust between 0 and 100%?

float thrustPercent;
    float brakesTorque;

    AircraftPhysics aircraftPhysics;
    Rigidbody rb;

    private void Start()
    {
        aircraftPhysics = GetComponent<AircraftPhysics>();
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        Pitch = Input.GetAxis("Vertical");
        Roll = Input.GetAxis("Horizontal");
        Yaw = Input.GetAxis("Yaw");

        if (Input.GetKeyDown(KeyCode.Space))
        {
            thrustPercent = thrustPercent > 0 ? 0 : 1f;
        }

        if (Input.GetKeyDown(KeyCode.F))
        {
            Flap = Flap > 0 ? 0 : 0.3f;
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            brakesTorque = brakesTorque > 0 ? 0 : 500f;
        }

        displayText.text = "S: " + ((int)rb.velocity.magnitude).ToString("D3") + " KTS\n";
        displayText.text += "A: " + ((int)transform.position.y * 3).ToString("D4") + " FT\n";
        displayText.text += "T: " + (int)(thrustPercent * 100) + "%\n";
        displayText.text += brakesTorque > 0 ? "B: ON" : "B: OFF";

That’s exactly what I mean! but is it possible to make this as an axis? so that I can in/decrease the throttle with 2 buttons or a joystick ? would be very nice!

You might be interested in this:

My physics are all working fine! but i only can turn on and off the trottle. I’d like to have a vaule between 0% and 100%.

This is the given plane:

and the second pic is my improved plane

7604980--944317--Screenshot 2021-10-16 202450.jpg
7604980--944323--Screenshot 2021-10-16 202038.png

1 Like

Here’s instructions on how to set up an axis in the input manager, I’ll assume you’ll have named it Throttle for code purposes:

thrustPercent = Mathf.Clamp01(thrustPercent + (Input.GetAxis("Throttle") * 2f * Time.deltaTime));

Change the 2f to however fast you want it to go from 0…1 if your axis is at the extreme. 2f is 0.5 seconds in this case.

This is my Pilot Kurt game:

https://kurtdekker.itch.io/pilotkurt

The above is a WebGL build, but check out how the throttle works with your mouse: that is exactly how it works on mobile touch with your fingers.

I did that with something I made called a VAButton, and you can get that in my ProximityButtons package (see below).

          // read the power slider (a VAButton... see ProximityButtons repo below)
        if (vab_power.fingerDown)
        {
            var vaby = vab_power.outputRaw.y;

// were you holding it previous frame?
            if (engineFingerDown)
            {
// actually adjust the power setting
                power += (vaby - engineVabY);

                power = Mathf.Clamp01(power);
            }

// track previous for delta
            engineVabY = vaby;
            engineFingerDown = true;
        }
        else
        {
            engineFingerDown = false;
        }

That’s it… when you place your finger nothing happens, but it instantly becomes “zero change” for sliding up or down from 0 to 1 power.

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

https://gitlab.com/kurtdekker/proximity_buttons

https://sourceforge.net/projects/proximity-buttons/

This is the setup for a VAButton in Pilot Kurt, but there’s other examples in PRoximityButtons above:

  // sz and sz2 are screen pixel dimensions of rect from my screen layout package
        vab_power = gameObject.AddComponent<VAButton>();
        vab_power.r_downable = new Rect( Screen.width - sz2, Screen.height - (sz + sz2), sz2, sz);
        vab_power.label = "^\n^\npower\n^\n^";
        vab_power.doClamp = true;
        vab_power.doNormalize = false;

EDIT after seeing Grozz’ post above:

My controls do not have any intentional lag: you can slam from 0 to 1 instantly with the power lever.

If I wanted, I would use this pattern to introduce lag (engine momentum) AFTER the control intention is gathered, unchanged, the way it is above.

Smoothing movement between any two particular values:

https://discussions.unity.com/t/812925/5

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

I’ve set up the code like this:

        if (Input.GetKeyDown(KeyCode.Space))
        {
            thrustPercent = Mathf.Clamp01(thrustPercent + (Input.GetAxis("Throttle") * 2f * Time.deltaTime));
        }

And the imput manager like this:

7606522--944599--upload_2021-10-27_13-25-16.png

It doesn’t work but I think that’s the code with the space imput…

Now I tried a few things and I get the error: (50,13): error CS0029: Cannot implicitly convert type ‘float’ to ‘bool’

7606522--944590--upload_2021-10-27_13-22-10.png
7606522--944599--upload_2021-10-27_13-25-16.png

That’ll only do anything on the same frame that Space is pressed, you want GetKey() if you want them to have to hold Space then press 1 or 2.

If I’ve everything right, a slider should appear:
7606567--944611--upload_2021-10-27_13-48-5.png

how can I change that?

I get these 2 errors:

Now I tried a few things but it still doesn’t work (2errors) —^ :
I’ve makred everything I added green, so you can see…
[Range(0, 100)]
public float Throttle;
[Range(0, 1)]
public float Flap;
[SerializeField]
Text displayText = null;
float thrustPercent;
float brakesTorque;
AircraftPhysics aircraftPhysics;
Rigidbody rb;
private void Start()
{
aircraftPhysics = GetComponent();
rb = GetComponent();
}
private void Update()
{
Pitch = Input.GetAxis(“Vertical”);
Roll = Input.GetAxis(“Horizontal”);
Yaw = Input.GetAxis(“Yaw”);
Throttle = Imput.GetAxis(“Throttle”);
if (Input.GetAxis(“Throttle”))
{
thrustPercent = Mathf.Clamp01(thrustPercent + (Input.GetAxis(“Throttle”) * 2f * Time.deltaTime));
}
if (Input.GetKeyDown(KeyCode.F))
{
Flap = Flap > 0 ? 0 : 0.3f;
}
if (Input.GetKeyDown(KeyCode.B))
{
brakesTorque = brakesTorque > 0 ? 0 : 500f;
}
displayText.text = “S: " + ((int)rb.velocity.magnitude * 1).ToString(“D3”) + " KTS\n”;
displayText.text += “A: " + ((int)transform.position.y * 3).ToString(“D4”) + " FT\n”;
displayText.text += "T: " + (int)(thrustPercent) + “%\n”;
displayText.text += brakesTorque > 0 ? “B: ON” : “B: OFF”;
}

GetAxis() doesn’t return a boolean, it returns the axis’ current value between -1 and 1. You don’t “check” if an axis is pressed like you do a regular button. You don’t need the if statement.

I tried many changes but it doesn’t work.

I get This error: Assets\Aircraft Physics\Example\Scripts\AirplaneController.cs(48,20): error CS0103: The name ‘Imput’ does not exist in the current context

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AirplaneController : MonoBehaviour
{
    [SerializeField]
    List<AeroSurface> controlSurfaces = null;
    [SerializeField]
    List<WheelCollider> wheels = null;
    [SerializeField]
    float rollControlSensitivity = 0.2f;
    [SerializeField]
    float pitchControlSensitivity = 0.2f;
    [SerializeField]
    float yawControlSensitivity = 0.2f;

    [Range(-1, 1)]
    public float Pitch;
    [Range(-1, 1)]
    public float Yaw;
    [Range(-1, 1)]
    public float Roll;
    [Range(0, 1)]
    public float Flap;
    [Range(0, 1)]
    public float Throttle;
    [SerializeField]
    Text displayText = null;

    float thrustPercent;
    float brakesTorque;

    AircraftPhysics aircraftPhysics;
    Rigidbody rb;

    private void Start()
    {
        aircraftPhysics = GetComponent<AircraftPhysics>();
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        Pitch = Input.GetAxis("Vertical");
        Roll = Input.GetAxis("Horizontal");
        Yaw = Input.GetAxis("Yaw");
        Throttle = Imput.GetAxis("Throttle");

    
        thrustPercent = Mathf.Clamp01(thrustPercent + (Input.GetAxis("Throttle") * 2f * Time.deltaTime));
        

        if (Input.GetKeyDown(KeyCode.F))
        {
            Flap = Flap > 0 ? 0 : 0.3f;
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            brakesTorque = brakesTorque > 0 ? 0 : 500f;
        }

        displayText.text = "S: " + ((int)rb.velocity.magnitude * 1).ToString("D3") + " KTS\n";
        displayText.text += "A: " + ((int)transform.position.y * 3).ToString("D4") + " FT\n";
        displayText.text += "T: " + (int)(thrustPercent) + "%\n";
        displayText.text += brakesTorque > 0 ? "B: ON" : "B: OFF";
    }

    private void FixedUpdate()
    {
        SetControlSurfecesAngles(Pitch, Roll, Yaw, Flap);
        aircraftPhysics.SetThrustPercent(thrustPercent);
        foreach (var wheel in wheels)
        {
            wheel.brakeTorque = brakesTorque;
            // small torque to wake up wheel collider
            wheel.motorTorque = 0.01f;
        }
    }

    public void SetControlSurfecesAngles(float pitch, float roll, float yaw, float flap)
    {
        foreach (var surface in controlSurfaces)
        {
            if (surface == null || !surface.IsControlSurface) continue;
            switch (surface.InputType)
            {
                case ControlInputType.Pitch:
                    surface.SetFlapAngle(pitch * pitchControlSensitivity * surface.InputMultiplyer);
                    break;
                case ControlInputType.Roll:
                    surface.SetFlapAngle(roll * rollControlSensitivity * surface.InputMultiplyer);
                    break;
                case ControlInputType.Yaw:
                    surface.SetFlapAngle(yaw * yawControlSensitivity * surface.InputMultiplyer);
                    break;
                case ControlInputType.Flap:
                    surface.SetFlapAngle(Flap * surface.InputMultiplyer);
                    break;
            }
        }
    }

    private void OnDrawGizmos()
    {
        if (!Application.isPlaying)
            SetControlSurfecesAngles(Pitch, Roll, Yaw, Flap);
    }
}

7606882--944656--upload_2021-10-27_15-16-56.png

Come on, read the error!!!

It’s telling you exactly what the problem is.

Imput is NOT Input

Oh my gosh how… bruh. thanks!
but now I’ve one last error

(50,13): error CS0029: Cannot implicitly convert type ‘float’ to ‘bool’ I don’t know what that means.

if (Input.GetAxis("Throttle"))
        {
            thrustPercent = Mathf.Clamp01(thrustPercent + (Input.GetAxis("Throttle") * 2f * Time.deltaTime));
        }

50:
51:
52:
53: