One Finger Touch Only with Input.GetMouseButton?

Hi, iam trying to move gameobject using this script. I want exactly this type of movement where player can touch anywhere on the screen and move gameobject along x axis from that touch position. I don’t want gameobject to move toward touch position or “jump” to touch position. My problem with this code is if player accidentally or purposely use second finger while controlling gameobject, gameobject get odd behavior. I dont want that. I want gameObject to controlled by only one finger.

I have been working on all day on this, i have tried also if(buttonIsPressed) dosomething

    void NewMouseMove()
    {
        if (Input.GetMouseButtonDown(0))
        {
            screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
            offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(
                Input.mousePosition.x,
                Input.mousePosition.y,
                screenPoint.z));
        }
        if (Input.GetMouseButton(0))
        {
            Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
            Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
            transform.position = Vector3.SmoothDamp(this.transform.position, new Vector3(
                curPosition.x,
                this.transform.position.y,
                this.transform.position.z), ref velocity, movementSmoth);
        }
    }

I tried this also, but i dont like the movement of this. And some time its lags little bit

void TouchControllTwo()
    {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(
                Input.GetTouch(0).position.x,
                Input.GetTouch(0).position.y,
                0));
        }
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            Vector3 curScreenPoint = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0);
            Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
            transform.position = Vector3.SmoothDamp(this.transform.position, new Vector3(
                curPosition.x,
                this.transform.position.y,
                this.transform.position.z), ref velocity, movementSmoth);
        }

    }

Can anyone please help me on this.

If you use Input.mousePosition on a multi-touch tablet, that will contain the average of all touching fingers. Thus when a second finger touches or grazes the screen, the apparent actual touch will jump to the average of the two.

If you want the game to be testable on PC, but also play on multi-touch, it’s handy to use the Application.platform variable to decide which you are on, and then each frame copy either Input.mouseTouch (for PC) or the first element in the Input.touches position over.

Then make your common code operate on that copied position (usually stored in a Vector3).

1 Like

@Kurt-Dekker Thank you for reply, so there is no way of making one touch control with mousePosition?. :frowning:

    void TouchControlTwo()
    {
        if (Input.touchCount == 1)
        {
            if (Input.GetButtonDown("Fire1"))
            {
                screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
                offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
            }

            if (Input.GetButton("Fire1"))
            {
                Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
                Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
                //transform.position = new Vector3 (curPosition.x, this.transform.position.y, this.transform.position.z);
                //transform.position = Vector3.Lerp(this.transform.position, new Vector3(curPosition.x, this.transform.position.y, this.transform.position.z), 0.2f);
                transform.position = Vector3.SmoothDamp(this.transform.position, new Vector3(curPosition.x, this.transform.position.y, this.transform.position.z), ref velocity, movementSmoth);
            }
        }
    }

This code stops the gameobject when player touch with second finger :frowning:

one question: you want to move the character from touch position or from its current position along x-axis.
second: gameobject should continue move in x-directon till finger kept pressed and stop moving once you release your finger?

Hi @lazydevx

Yes! If finger is pressed and moving

I want to move object along its current x position
Like this
https://www.youtube.com/watch?v=K0nY_v7CH2Q

Please Please i have been struggling for long time now with this.

try this once
public float REFRESH_TIME = 0.1f;
float refreshDelta = 0.0f;
void NewMouseMove()
{
float smoothingTime = 10.0f; //decrease value to move faster
if (Input.GetMouseButtonDown(0)) {

touchStartPos = Input.mousePosition;
refreshDelta = 0.0f;
}
if (Input.GetMouseButton(0)) {

refreshDelta += Time.deltaTime;
Vector3 diff = (Input.mousePosition - touchStartPos);
Vector3 finalPos = transform.position + diff;

if (refreshDelta >= REFRESH_TIME) {

refreshDelta = 0.0f;
touchStartPos = Input.mousePosition;
}

transform.position = Vector3.Lerp (transform.position, new Vector3 (finalPos.x, transform.position.y, transform.position.z), Time.deltaTime / smoothingTime);

}
}

Thank you @lazydevx but i dint like the movement of this. I really liked my code movement but,

@Kurt-Dekker has the right of it. But I think you don’t actually need to check the platform; you can just check touchCount, and if that’s > 0, use the first touch, otherwise use the mouse. Like so:

void Update() {
    if (Input.touchCount > 0) MoveTo(Input.GetTouch(0).position);
    else MoveTo(Input.mousePosition);
}

void MoveTo(Vector2 position) {
    // here, do with 'position' whatever you were doing with Input.mousePosition before!
}

Please give it a try. I think it will work.

Hi, Thank you again @JoeStrout , but i don’t know how to implement your code to my mousePosition code, i have many if-statement,

Anyway i think i have created code iam happy with, When i tried first withInput.GetTouch(0).phase == TouchPhase.Moved

I was getting odd behavior, some kind of flickering to my gameObject, so i found this thread

I dont know if its bug or just wrong with my code. But when i use

Input.GetButton ("Fire1")

Works just as i wanted, but player can mess up gameplay if using two fingers. So i had to come with another solution to this.

 void TouchControl()
    {
        if (Input.touchCount>0)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
                screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
                offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPoint.z));
                touchHasBegan = true;
            }
            if (Input.GetTouch(0).phase == TouchPhase.Ended)
            {
                touchHasBegan = false;
                //Set to true so ball can jump when player release to finger from touchscreen
                canJump = true;
               
            }
            if (Input.GetButton("Fire1")&&touchHasBegan)
            {
                Vector3 curScreenPoint = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, screenPoint.z);
                Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
                transform.position = Vector3.SmoothDamp(this.transform.position, new Vector3(curPosition.x, this.transform.position.y, this.transform.position.z), ref velocity, movementSmoth);
            }

        }
    }

So far this works good.

So, question. Do you need multitouch at all?
You can disable multitouch with

Input.multiTouchEnabled = false;

Just call this in a starting scene to disable multitouch if you don’t ever need it. I know I use this for a puzzle game where you drag pieces around and needed it to stay with the first touch.

You could also turn it on or off probably if you have different scenes that use multitouch and some that don’t.

4 Likes

@Brathnann what``??? is this possible with Input from mouse?
Edited
Tested,
Nope i cant use Input.GetButton and turn of mulitTouch

I use the same code for mouse and touch. I am using the event system though for dragging pieces around. (OnPointerDown, OnDrag, etc) We had the same issue where a person could tap one finger and then another finger and the puzzle piece jumped across screen. So by using that line of code, Unity only uses the first touch. So now the piece sticks to the first finger touch no matter how many touches the player does.

I don’t see why it wouldn’t work for you. I think all it does is disable multitouch.

OMG, yes its working @Brathnann ann For 3 days i have been struggling with this

THANK YOU SO MUCH @Brathnann

1 Like

Glad I could help! And good to see it works with the other system as well.