stefanplc
stefanplc

Reputation: 439

Unity3d simulate GetAxis

I'm trying to simulate the acceleration of Input.GetAxis("Horizontal") and Input.GetAxis("Vertical") based on where I'm touching the screen. So lets say I have an object in the middle of my screen called "middle_Object". What I'd like to do is if I touch to the right of the object, to simulate the gradual increase from 0 to 1 that Input.GetAxis("Horizontal") does and then if I put my finger to the left, it quickly goes back to 0 and then gradually decreases to -1. Basically transform the input.getaxis into a touch friendly version. Any ideas how I could do this?

Thanks!

Upvotes: 2

Views: 1358

Answers (1)

scotty5e
scotty5e

Reputation: 46

It sounds like you need the magic of Lerp :) (linear interpolation)

Essentially, you want to work out if you're to the left or right (or up or down) of a reference point. In this case, let's say the centre of the screen. If you are, move toward either 1 or -1 accordingly.

On a touch screen, this means unless you add a 'deadzone', you will never be at zero, which is a pain, so you should also check if the distance from the centre is too small to care.

Then, you use the Lerp function to go from where you are now, to where you want to be, at the speed of your choice.

Here's some code with comments to show you how I'd do that.

// for this example, using the actual screen centre as our central point
// change this if you like :)
Vector2 myCentre = new Vector2( Screen.width/2, Screen.height/2 );
Vector2 touchPos = new Vector2( Screen.width/2, Screen.height/2 );

// change this to affect how quickly the number moves toward its destination
float lerpSpeed = 0.3f;

// set this as big or small as you want. I'm using a factor of the screen's size
float deadZone = 0.1f * Mathf.Min( Screen.width, Screen.height );   

public void Update()
{
    Vector2 delta = (Vector2)Input.mousePosition - myCentre;

    // if the mouse is down / a touch is active...
    if( Input.GetMouseButton( 0 ) == true )
    {
        // for the X axis...
        if( delta.x > deadZone )
        {
            // if we're to the right of centre and out of the deadzone, move toward 1
            touchPos.x = Mathf.Lerp( touchPos.x, 1, lerpSpeed );
        }
        else if( delta.x < -deadZone )
        {
            // if we're to the left of centre and out of the deadzone, move toward -1
            touchPos.x = Mathf.Lerp( touchPos.x, -1, lerpSpeed );
        }
        else
        {
            // otherwise, we're in the deadzone, move toward 0
            touchPos.x = Mathf.Lerp( touchPos.x, 0, lerpSpeed );
        }

        // repeat for the Y axis...
        if( delta.y > deadZone )
        {
            touchPos.y = Mathf.Lerp( touchPos.y, 1, lerpSpeed );
        }
        else if( delta.y < -deadZone )
        {
            touchPos.y = Mathf.Lerp( touchPos.y, -1, lerpSpeed );
        }
        else
        {
            touchPos.y = Mathf.Lerp( touchPos.y, 0, lerpSpeed );
        }
    }
    else
    {
        // the mouse is up / no touch recorded, so move both axes toward 0
        touchPos.x = Mathf.Lerp( touchPos.x, 0, lerpSpeed );
        touchPos.y = Mathf.Lerp( touchPos.y, 0, lerpSpeed );
    }

    Debug.Log("TouchPos: " + touchPos );

}

Upvotes: 2

Related Questions