Turning my player using a floating joystick

Currently im using keys a d to rotate my player left and right
and w and s to accelerate and slow down.

But this doesnt actually work on android obviosly.so i downloaded the joystick pack and put a floating joystick on after changing the build to android.

this line takes in the key press

if (Input.GetButton(“TurnShipRight”))
{
TurningRight();
}

How can i apply the joystick to do this job instead.

Epoch! Mr Loop-De-Loop… how’s it going!

The part you’re missing is this bit:

Last I saw your game, you had buttons to either “rotate clockwise” or “rotate counterclockwise”

A Joystick doesn’t really give you that kind of input.

You must choose how to map the joystick to do so.

In my sopwith camel game fiddlings I did it by making “up joystick” map to nose over, and “down joystick” map to pull up, sort of like a real airplane.

You could alternately put two buttons onscreen and read them that way…

OR… you could read the X, Y of the joystick, turn it into an angle, compare the angle to your current flight angle, and then decide “do I need to nose over or pull up?”

Either way you go, that’s the gap you have: discreet input vs continous input.

1 Like

i will just use buttons probarbly much easier
happy new year Kurt!!!

I haven’t looked at the joystick pack, but each axis of the joystick probably returns a float. Add some Debug.Log statements to take a look at what the axis outputs in each direction. For example, if the axis returns 0f at center, 1f in one direction and -1f in the far other direction, you could then do some simple “if” statements. Such as if the axis is greater than 0.2f, then do one thing, less than -0.2f do the opposite direction, leaving a little play in the center so it isn’t overly sensitive. That’s probably how I’d approach it at least.

1 Like

Back at you Epoch!

In other news, I have something called a ProximityButtonSet… it’s a collection of buttons so you can slide your finger back and forth between them and it smoothly considers only one or the other… this MIGHT be better. You would make a turn-right and turn-left button in one set.

You can clone the project and see how they work… try out the DemoProximityButtonsOnGUI.unity scene. You can even build it to your touch / phone / tablet and see how it feels using that scene.

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

1 Like

thanks so much

Hey kurt how does the button set in the space shooter demo get created?

It is in the SpaceShooterPlayerController, this function:

    void CreateVABs()
    {
        if (vabMove) Destroy( vabMove);
        if (pbsWeapons) Destroy(pbsWeapons);

        vabMove = gameObject.AddComponent<VAButton>();
        vabMove.r_downable = MR.SR( 0.00f, 0.20f, 0.50f, 0.80f);
        vabMove.doClamp = true;
        vabMove.doNormalize = false;
        vabMove.label = "MOVE";

        float sz = Mathf.Min(Screen.width, Screen.height) * 0.35f;
        pbsWeapons = ProximityButtonSet.Create(sz);
        pbFire1 = pbsWeapons.AddButton("FIRE1", MR.SR(0.7f, 0.9f, 0, 0).center);
        pbFire2 = pbsWeapons.AddButton("FIRE2", MR.SR(0.9f, 0.8f, 0, 0).center);

    }

Specifically the last 4 lines make the button set and add 2 buttons to it.

VAB is the joystick (Virtual Analog button)

This function should be renamed to CreateControls() really. :slight_smile:

tried to add this to my player script

VAButton vabMove;
    ProximityButtonSet pbsWeapons;
    ProximityButtonSet.ProximityButton pbFire1;
    ProximityButtonSet.ProximityButton pbFire2;

    void CreateVABs()
    {
        if (vabMove) Destroy(vabMove);
        if (pbsWeapons) Destroy(pbsWeapons);

        vabMove = gameObject.AddComponent<VAButton>();
        vabMove.r_downable = MR.SR(0.00f, 0.20f, 0.50f, 0.80f);
        vabMove.doClamp = true;
        vabMove.doNormalize = false;
        vabMove.label = "MOVE";

        float sz = Mathf.Min(Screen.width, Screen.height) * 0.35f;
        pbsWeapons = ProximityButtonSet.Create(sz);
        pbFire1 = pbsWeapons.AddButton("FIRE1", MR.SR(0.7f, 0.9f, 0, 0).center);
        pbFire2 = pbsWeapons.AddButton("FIRE2", MR.SR(0.9f, 0.8f, 0, 0).center);

    }

but this has an error

VAButton

i cant find the component

ok was because there were scripts missing

Why do i feel like i cant code and am constantly trying to thread things together to get them working for myself. Its a terrible feeling.

If you don’t need the VAButton, delete the lines of code asking for it, assuming you want two buttons.

That’s all anybody does when starting out… keep after it, keep looking for patterns, and if you do something and it works, STOP STOP STOP STOP and go back and understand 100% of why it works.

Otherwise you are just doing a raindance (dancing until rain happens… but your dance had nothing to do with making it rain). Understand what you’re doing always before moving forward.

1 Like

Thanks do want buttons

Hi Kurt what are the scripts from the pack you made available that i need to just get the “joystick” and a few buttons going, there is a lot of stuff in there and cant work out what is needed.

Curious, which joystick pack are you using?

Also, if the values are -1.0 to 1.0 for directions, you can try Mathf.Pow() and tweak the exponent. An exponent of 1 is linear (default, no change), but if you go up, the sensitivity from the middle outwards drops: 0.25 becomes 0.0625, 0.5 becomes 0.25, but 1 stays 1, at the max distance from center. This has a better feel because small movements near the center don’t have an outsized effect. Note that if the initial value is negative, do the Mathf.Pow() and then multiply by -1 afterwards to make it negative again.

It is all super-straightforward stuff, you should challenge yourself to take apart one or two of the example scenes and see what each part does. It’s easy: try commenting parts of it out, making sure there are no errors, then running it, and that way you can learn what each part does.

ok im on it

1 Like

Hi Kurt

What is
DSM.GameRunning.bValue = true;

What is DSM? i know its a class, but where is it? I cant find a script called DSM.

any idea?

That’s part of the Datasacks module, which is included as a frozen-in-time copy of it in the proximity_buttons project. Check the urls.txt for where to see more info about it. If you’re copying the code out to your project, just delete those lines.

Okay so i have the controller appearing on my project
but its doing nothing
My player moves the way i want using the keys

 if (Input.GetButton("TurnShipRight"))
        {
            TurningRight();

//Which then does
desiredRotationSpeed = -playerTurnAmount;
        }

Looking at the space demo its working very differently.
It seems to be doing something on the SpaceShooterGameManager which i dont understand. In any case i cant see how to apply the space demo working solution to my project. Will it require a complete rewrite? Though I know thats a stupid question without you looking at my code.