I’m developing a simple mobile game where you have to move the object left and right by tapping or holding one of the two halves of the screen but i don’t know how to do that, I didn’t found out any tutorials that explain exactly what I’m looking for. Can you help me please?
Thanks
There are tons of examples of this across the internet, so you might want to work on your web searching skills.
You can start with checking out the documentation for Touch input events.
If you want to know if any touches are currently happening, you can check Input.touchCount, which tells you the number of touches happening.
You can get info about a specific touch by using Input.GetTouch(0). The zero means the first touch, if you wanted the second touch you would give one as the parameter.
Then to see what the touch is doing this frame, you can use TouchPhase.
So here’s an example of getting the touch and checking which side of the screen its on.
using UnityEngine;
public class HalfScreenTouchMovement : MonoBehaviour
{
private float screenCenterX;
private void Start()
{
// save the horizontal center of the screen
screenCenterX = Screen.width * 0.5f;
}
private void Update()
{
// if there are any touches currently
if(Input.touchCount > 0)
{
// get the first one
Touch firstTouch = Input.GetTouch(0);
// if it began this frame
if(firstTouch.phase == TouchPhase.Began)
{
if(firstTouch.position.x > screenCenterX)
{
// if the touch position is to the right of center
// move right
}
else if(firstTouch.position.x < screenCenterX)
{
// if the touch position is to the left of center
// move left
}
}
}
}
}