Home > Software engineering >  How to add touch controls unity
How to add touch controls unity

Time:07-30

I was creating a flappy bird game in Unity and while I was trying to add touch inputs it gave me this error

Unity project

InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.

I think it's because I downloaded the new Input system from the package manager, but i removed it

playerScript Code:

using UnityEngine;

public class playerScript : MonoBehaviour
{
    Rigidbody2D rb;
 
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0) || Input.touchCount > 0)
        {
            rb.velocity = new Vector2(0f, 7f);
        }
    }
}

CodePudding user response:

Solved it!

You need to go to the Player settings Build settings/Player settings/Other settings/Configuration and search for:

enter image description here

and set Input manager old

CodePudding user response:

Try change code to this

using UnityEngine;

public class playerScript : MonoBehaviour
{
Rigidbody2D rb;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
}

private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Jump();
    }
}
public void Jump()
  {
     rb.velocity = new Vector2(0f , 7f);
  }

}

Then add UI button and on the on click drag the gameobject this script is attacked to into it, click the script and then click 'Jump()'

CodePudding user response:

You could add Giant UI button and add function jump and make it work when button is pressed. I had similar problem for visual novel game. And I added screen sized button :)

  • Related