Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Mobile movement with ramping up floats

Discussion in '2D' started by xDayhawk, Jan 14, 2023.

  1. xDayhawk

    xDayhawk

    Joined:
    Jun 24, 2020
    Posts:
    55
    Hi all,

    I've been creating games using Unity for quite some time, and for the first time i'm trying to switch my focus to the mobile theme.

    Made a very simple platformer, and after a lot of investigations, i just can't get my head around how to achieve the same results as i'm seeing with Unity's input system, the old input system.

    When i'm using
    Code (CSharp):
    1. Input.GetAxis("Horizontal");
    and i'm pressing on a button i get a small ramp-up of a -1 to 1 float, when i'm using the new input system with touch on mobile i'm always getting 1 or -1 without any ramp up which cases the player to just shoot out running instead of slightly ramping up.

    How do i get the same effect in mobile? pressing on a button and slowly getting 0-1 or 0 -(1) ?

    My actual movement code isn't complicated its just this (in fixedUpdate of course)
    Code (CSharp):
    1. rigidBody2d.velocity = new Vector2(_horizontal * _movementSpeed, rigidBody2d.velocity.y);
    Hope my question in clear, if more info is needed please let me know.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    You can smoothly transition ANY quantity yourself. Here's how:

    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    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
     
  3. xDayhawk

    xDayhawk

    Joined:
    Jun 24, 2020
    Posts:
    55
    @Kurt-Dekker thanks so much for you answer i'll try to implement it.
    So that's basically the difference between the old input system's "GetAxis" and "GetAxisRaw"? that raw that's have that smoothing and the regular one has?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Correct!

    I personally ALWAYS use
    Input.GetAxisRaw()
    because I find it more centralized to control the "smoothness" in code (always using the above thingy I posted) than to control the smoothness in the InputManager, which of course is totally global to the entire project.

    I might use
    Input.GetAxis()
    in simple demos and one-offs though.

    And sometimes you want even more-custom behaviour, like smoothing as long as are gathering speed, but if you are REVERSING yourself you might want much higher rate of change of the direction to make the game feel snappier.

    This type of custom input filtering and tuning it well is pretty much what sets apart incredibly polished platformers (such as pretty much EVERYTHING from Nintendo!) from less-polished games.
     
  5. xDayhawk

    xDayhawk

    Joined:
    Jun 24, 2020
    Posts:
    55
    I definitely see your point, its a good one.
    and i love the Nintendo comment :)

    Thanks for the tip, it does make a lot of sense!
     
    Kurt-Dekker likes this.