Search Unity

Resolved Customize On-Screen Stick

Discussion in 'Input System' started by CFodder1977, Nov 17, 2020.

  1. CFodder1977

    CFodder1977

    Joined:
    Nov 18, 2018
    Posts:
    7
    I really like the simplicity of the On-Screen Stick script, especially in terms of how easy it is to incorporate the new Input system with it via the "Control Path" setting. However, is there a way to customize the current script so that the player's movement range of the stick input is not restricted to a circular motion?

    For example, when a player slides the stick all the way to the right and then begins dragging upwards, the stick image arcs up and to the left (i.e. the "x" vector gets smaller as the "y" axis climbs higher). I would rather the "x" value just remain whatever it was when the player started dragging upward (or at least be whatever value equals where the player's finger is touching on the screen restricted to a square bounded by the "Movement Range" setting vs a radius).

    Is it possible to edit the existing OnScreenStick code to accomplish this? If not, is there a different component or asset I should be using instead?
     
    Last edited: Nov 17, 2020
  2. CFodder1977

    CFodder1977

    Joined:
    Nov 18, 2018
    Posts:
    7
    I ended up figuring out a solution if anyone is interested. Within the OnScreenStick script, do the following:

    Replace this line of code:
    Code (CSharp):
    1. delta = Vector2.ClampMagnitude(delta, movementRange);
    With this code instead:
    Code (CSharp):
    1. delta.x = Mathf.Clamp(delta.x, -movementRange, movementRange);
    2. delta.y = Mathf.Clamp(delta.y, -movementRange, movementRange);
    The original "ClampMagnitude" code restricts motion to a radius whereas simply using a "Mathf.Clamp" allows the x and y motion to utilize the full range of the "movementRange" value. This will give you something more akin to a touchpad vs a control stick.
     
    Last edited: Nov 17, 2020