How do I get smooth movement with the joystick, like the keyboard?

I don’t know if anyone has compared the two, but if you play the game with the keyboard, the movement is more fluid and smooth. When I play with the joystick, for example, I move the joystick up and then release, the ship goes up and then stops abruptly.

Watch the video to see what I mean. The first set of movements is with the keyboard, the second is with the joystick.

How do I apply the same fluid movements from the keyboard, to the joystick?

2 Likes

Hi Jeremy,

You could test the GetAxisRaw method instead of the GetAxis method.

1 Like

Hi Nina! Thanks for the suggestion. I’ll try it out later today and see if it solves the problem.

1 Like

Hi Nina, I’ve tried making the changes, but the effect was undesirable. It seemed to have no effect on the joystick and made the keyboard controls less fluid. This was the code I changed:

        xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        yThrow = CrossPlatformInputManager.GetAxis("Vertical");

and I changed it to this:

        xThrow = CrossPlatformInputManager.GetAxisRaw("Horizontal");
        yThrow = CrossPlatformInputManager.GetAxisRaw("Vertical");

I’m not sure if it’s clear what I’m asking so I’d like to try again and explain what I’m trying to do. When I’m using the keyboard to play pressing the “W” key to move the aircraft up… when I let go of the key, in continues to go up a little, slows down causally, straightens out, and then comes to a stop. When I’m on my gamepad, pressing up on the left joystick, the aircraft moves up. I release the joystick control, but instead of the fluid movement it quickly comes to a stop. I’d like to apply the smooth animation of the aircraft when using the keyboard, to the joystick, so that both controls, move the aircraft fluidly.

1 Like

I think I got now what you mean.

Could you share the entire code which is responsible for moving and rotating the ship? Did you use Time.deltaTime?

What you could try is to use the FixedUpdate method and wrap the code for the movement into an if-block. The conditon would be a bool which gets set to either true or false in Update. Use the AddForce method of the Rigidbody component of the ship.

Also see this answer in the Unity forum.

Maybe you’ll have to scrap the current solution and write your own one with a coroutine which rotates the ship back to its initial angle. In this case, it could make sense to use GetAxisRaw and round the value to either -1, 0 or 1 and have your coroutine calculate the rest. This way, you’ll have full control, and the specifics of the input device will not affect anything.

1 Like

I think the link you provided is what I’m looking for. In his answer:

Your Edit > Project Settings > Input Settings should be adjusted to taste.

Specifically the Gravity (how quickly the input returns to zero after the player releases it) and Acceleration (how quickly the input arrives at the actual value the player is inputting) options of your axes.

I think the Gravity setting is what I’m looking for, “how quickly the input returns to zero after the player releases it” is what I want to be slower. It happens to quickly.

I’ll look at the settings and see if I can tweak it to my liking. If I remember, there should be two horizontal and vertical settings, one for keyboard and one for joystick.

Thanks so much for your help!!

1 Like

Welp! Looks like I spoke too soon! The link seemed to make sense and is what I want, to adjust the time it takes to return to 0 after letting go of the stick, but it seems to have little effect on the my joystick, either that, or it’s just not reacting in the same way as the keyboard. I think I would have to write my own code as you suggested to fix the issue. I might have to move on, as I’m not sure I’m experienced enough to write the code on my own. I’m pretty sure it’s probably the rotation I’m wanting to fix.

Here is what my current code looks like:

private void ProcessTranslation()
{
xThrow = CrossPlatformInputManager.GetAxis(“Horizontal”);
yThrow = CrossPlatformInputManager.GetAxis(“Vertical”);

    float xOffset = xThrow * Speed * Time.deltaTime;
    float yOffset = yThrow * Speed * Time.deltaTime;

    float rawXPos = transform.localPosition.x + xOffset;
    float rawYPos = transform.localPosition.y + yOffset;

    float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
    float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);

    transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
}

}

    private void ProcessRotation()
    {
        float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
        float pitchDueToControlThrow = yThrow * controlPitchFactor;
        float pitch = pitchDueToPosition + pitchDueToControlThrow;

        float yaw = transform.localPosition.x * positionYawFactor;

        float roll = xThrow * controlRollFactor;

        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
    }
1 Like

The keys on your keyboards have two states: on (1) and off (0). Your joystick knows off (0), max (1) and something in between.

Before you write any fancy solutions, try to apply the “on”/“off” concept to the joystick. Everything above or below 0 is “on”. Use GetAxisRaw because it might be that GetAxis works well with the keyboard only. You cannot affect the GetAxis method from outside, so you’ll have to develop your own solution.

Once both input devices give the same “undesired” result, refine the behaviour. Do NOT try to make one look good because of the different values the devices return.

1 Like

Ahhhhhh!! That makes sense and that solution should be simple! I’ll give it a try!

1 Like