Input.mousePosition have issue for use Touch devices : please help

Hello there ,
i’m using same script or functions as mentioned in " HAT TRICK" Game by unity… have issue with input.

i want to use same functions : where’s the user drag or click on screen object move to that point…but if there’s user off-touch object move to their default position.

i want when user once touch to screen object follow with dragging only…and while not touch object pause to their last position :: hope you getting me-

here controller script
here’s script :

using UnityEngine;
using System.Collections;

public class HatController : MonoBehaviour {

public Camera cam;

private float maxWidth;
private bool canControl;

// Use this for initialization
void Start () {
	if (cam == null) {
		cam = Camera.main;
	}
    canControl = false;
    Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
	Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
	float hatWidth = GetComponent<Renderer>().bounds.extents.x;
	maxWidth = targetWidth.x - hatWidth;
}

// Update is called once per physics timestep
void FixedUpdate () {
    if (canControl)
    {
        Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
        Vector3 targetPosition = new Vector3(rawPosition.x, -3.5f, 0.0f);
        float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
        targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);
        GetComponent<Rigidbody2D>().MovePosition(targetPosition);
    }

}
public void ToggleControl(bool toggle)
{
    canControl = toggle;
}

}

Please help Asap :slight_smile:

I don’t think you can use Input.mousePosition on mobile. However you can check for touches using a foreach loop for all touches like the following

    foreach(Touch touch in Input.touches) {
    //code for if touches are present
    }

in there you can check the Input.touchPhase to determine if it was canceled, moved, exited, or started.
The following is how I check the touch phases:

if (touch.phase == TouchPhase.Began) {
                        recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Ended) {
                        recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Stationary) {
                        recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Canceled) {
                        recipient.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

Either inside the for-each loop or the touch phase if statements you can then use touch.position
@jemke I hope that this will help solve your issues.