screen touching

Hi, i’m doing a little mobile project…
How can i make sure that if I’m touching the right part of the screen the object performs an action while if I touch the left part the object makes another action??

Thank you!!!

You’ll need to get the point where your touch is and check the x value to see if it’s on the left or right side.

here is a good start for you.

void Update()
{
    for (int i = 0; i < Input.touchCount; i++)
    {
        if (Input.GetTouch(i).phase == TouchPhase.Began)
        {
            Vector3 p = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
            Debug.Log(p);
           
            if(p.x < Screen.width/2)
            {
                //touched on the left
                //do other code
                Debug.Log("LeftTouch");
            }
            else
            {
                //touched on the right
                //do other code
                Debug.Log("Right Touch");
            }
        }
    }
}

if I do play the command does not work …
is it because the command works only on mobile devices?

That’s correct Input.GetTouch is for mobile devices, touch screen.

So it’s not going to work when testing with the play button

A better approach (in my opinion) would be to just throw some buttons onto a Canvas, and position/lock them as desired.

If you don’t want these buttons to be visible, just delete any texture under them (and set their color to clear). They will still function normally even without any visible display.

try this. it might not work. I free handed the code. that means i didn’t test it

void Update()
{
//PC get mouse position
    if (Input.GetMouseButtonDown(0))
    {
        Vector3 p = Camera.main.ScreenToWorldPoint(Input.GetMouseButtonDown(0).position);
            Debug.Log(p);
          
            if(p.x < Screen.width/2)
            {
                //touched on the left
                //do other code
                screenTouched(true);
            }
            else
            {
                //touched on the right
                //do other code
                screenTouched(false);
            }
    }

//Mobile Device Touch Screen
    for (int i = 0; i < Input.touchCount; i++)
    {
        if (Input.GetTouch(i).phase == TouchPhase.Began)
        {
            Vector3 p = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
            Debug.Log(p);
          
            if(p.x < Screen.width/2)
            {
                //touched on the left
                //do other code
                screenTouched(true);
            }
            else
            {
                //touched on the right
                //do other code
                screenTouched(false);
            }
        }
    }
}

void screenTouched(bool leftSideTouched)
{
    if(leftSideTouched)
    {
        Debug.Log("LeftTouch");
    }
    else
    {
        Debug.Log("Right Touch");
    }

}

Thank you so much…
there are some error in line 6, how can i fix ???

try changing it to
Vector3 p = Camera.main.ScreenToWorldPoint(Input.GetMouseButton(0).position);

Nope.
error CS1061: Type bool' does not contain a definition for position’ and no extension method position' of type bool’ could be found.

You need to use mouseposition

Vector3 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);

Hm… :slight_smile:
Maybe you want:

Vector3 mp = Input.mousePosition;
if(mp.x <= Screen.width / 2) {
 // something
 }
else {
  // other thing.
 }

… skipping the world position, I mean.

That might be true as well. I’m doing 20 things at work, so it was a brief post while a project was building. lol.

how do I make the object go left when I hold down the left half of the screen and go right when I touch the right half, while if I release the object back to the starting position??

public GameObject ObjectToMove;

void Update()
{
//PC get mouse position
    if (Input.GetMouseButton(0))
    {
        Vector3 mp = Input.mousePosition;
            Debug.Log(p);
        
            if(mp.x < Screen.width/2)
            {
                //touched on the left
                //do other code
                screenTouched(true);
            }
            else
            {
                //touched on the right
                //do other code
                screenTouched(false);
            }
    }
//Mobile Device Touch Screen
    for (int i = 0; i < Input.touchCount; i++)
    {
        if (Input.GetTouch(i).phase == TouchPhase.Began)
        {
            Vector3 p = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
            Debug.Log(p);
        
            if(p.x < Screen.width/2)
            {
                //touched on the left
                //do other code
                screenTouched(true);
            }
            else
            {
                //touched on the right
                //do other code
                screenTouched(false);
            }
        }
    }
}
void screenTouched(bool leftSideTouched)
{
    if(leftSideTouched)
    {
        Debug.Log("LeftTouch");
        ObjectToMove.transform.position += -Vector3.right * Time.deltaTime;
    }
    else
    {
        Debug.Log("Right Touch");
        ObjectToMove.transform.position += Vector3.right * Time.deltaTime;
    }
}

Thank you!
UnassignedReferenceException: The variable ObjectToMove of moverocket has not been assigned.
You probably need to assign the ObjectToMove variable of the moverocket script in the inspector.
moverocket.screenTouched (Boolean leftSideTouched) (at Assets/moverocket.cs:63)

How i can fix that??

there is no line 63 in the script I’ve provided. Can you please post your script.

when i use this script it works perfectly. for the mouse click.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moverocket : MonoBehaviour {

    public GameObject ObjectToMove;

    void Update()
    {
        //PC get mouse position
        if (Input.GetMouseButton(0))
        {
            Vector3 mp = Input.mousePosition;
            Debug.Log(mp);

            if (mp.x < Screen.width / 2)
            {
                //touched on the left
                //do other code
                screenTouched(true);
            }
            else
            {
                //touched on the right
                //do other code
                screenTouched(false);
            }
        }
        //Mobile Device Touch Screen
        for (int i = 0; i < Input.touchCount; i++)
        {
            if (Input.GetTouch(i).phase == TouchPhase.Began)
            {
                Vector3 p = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
                Debug.Log(p);

                if (p.x < Screen.width / 2)
                {
                    //touched on the left
                    //do other code
                    screenTouched(true);
                }
                else
                {
                    //touched on the right
                    //do other code
                    screenTouched(false);
                }
            }
        }
    }
    void screenTouched(bool leftSideTouched)
    {
        if (leftSideTouched)
        {
            Debug.Log("LeftTouch");
            ObjectToMove.transform.position += -Vector3.right * Time.deltaTime;
        }
        else
        {
            Debug.Log("Right Touch");
            ObjectToMove.transform.position += Vector3.right * Time.deltaTime;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moverocket : MonoBehaviour {

    public GameObject ObjectToMove;

    void Update()
    {
        //PC get mouse position
        if (Input.GetMouseButton(0))
        {
            Vector3 mp = Input.mousePosition;
            Debug.Log(mp);

            if(mp.x < Screen.width/2)
            {
                //touched on the left
                //do other code
                screenTouched(true);
            }
            else
            {
                //touched on the right
                //do other code
                screenTouched(false);
            }
        }
        //Mobile Device Touch Screen
        for (int i = 0; i < Input.touchCount; i++)
        {
            if (Input.GetTouch(i).phase == TouchPhase.Began)
            {
                Vector3 p = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
                Debug.Log(p);

                if(p.x < Screen.width/2)
                {
                    //touched on the left
                    //do other code
                    screenTouched(true);
                }
                else
                {
                    //touched on the right
                    //do other code
                    screenTouched(false);
                }
            }
        }
    }
    void screenTouched(bool leftSideTouched)
    {
        if(leftSideTouched)
        {
            Debug.Log("LeftTouch");
            ObjectToMove.transform.position += -Vector3.right * Time.deltaTime;
        }
        else
        {
            Debug.Log("Right Touch");
            ObjectToMove.transform.position += Vector3.right * Time.deltaTime;
        }
    }
}

is it a null ref? you didn’t set the gameobject that you’re moving left or right