Hisu
Hisu

Reputation: 95

Unity for Android - object not following touch inputs

I've just started learning Unity (using C#) and I'm trying to create script which will make object that script is attached to follow my touch inputs (basically follow my finger when I'm moving it around).

What I tried to do is:

EDIT: this is refreshed version what I have now.

using UnityEngine;
using System.Collections;
using System;

public class PlayerMovement : MonoBehaviour {


    private Vector3 fingerEnd;

    void Start () {

    }

    void Update () {

        foreach(Touch touch in Input.touches)
        {

            if (touch.phase == TouchPhase.Began)
            {
                fingerEnd = touch.position;
            }
            if (touch.phase == TouchPhase.Moved && (Math.Sqrt(Math.Pow(fingerEnd.x,2) + Math.Pow(fingerEnd.y,2)) - Math.Sqrt(Math.Pow(transform.position.x,2) + Math.Pow(transform.position.y,2))<100))
            {
                fingerEnd = touch.position;
                Vector3 worldCoordinates = Camera.main.ScreenToWorldPoint(fingerEnd);
                transform.Translate(worldCoordinates);
            }
        }

    }
}

I've also tried using Vector2.Lerp, but still my object isn't moving. It's probably very basic mistake but I don't see what's not working here.

I also tried to change

fingerEnd = touch.position;
Vector3 worldCoordinates = Camera.main.ScreenToWorldPoint(fingerEnd);
transform.Translate(worldCoordinates);

to

fingerEnd = touch.position;
transform.Translate(fingerEnd);

or to

fingerEnd = touch.position;
Vector3 worldCoordinates = Camera.main.ScreenToWorldPoint(fingerEnd);
transform.position = worldCoordinates;

or to

fingerEnd = touch.position;
transform.position = fingerEnd ;

with no effect.

EDIT: Ok, I've solved it. It was my mistake. In IF clause I've used touch.position (fingerEnd) values, before I knew this is position in pixels on screen. I compared it to transform.position which was in unity coordinates. It worked when I separated these 2 conditions from IF.

End version of code:

using UnityEngine;
using System.Collections;
using System;

public class PlayerMovement : MonoBehaviour {

    Vector3 worldCoordinates;

    void Start () {

    }

    void Update () {

        foreach(Touch touch in Input.touches)
        {

            if (touch.phase == TouchPhase.Began)
            {
                Debug.Log("Touch: " + touch.position);
            }
            if (touch.phase == TouchPhase.Moved) 
            {
                worldCoordinates = Camera.main.ScreenToWorldPoint(touch.position);

                if ((Math.Sqrt(Math.Pow(worldCoordinates.x,2) + Math.Pow(worldCoordinates.y,2)) - Math.Sqrt(Math.Pow(transform.position.x,2) + Math.Pow(transform.position.y,2))<0.2))
                {
                    Debug.Log("Touch: " + touch.position);
                    Debug.Log("Transform: " + transform.position);
                    Debug.Log("WorldCoordinates: " + worldCoordinates);
                    //transform.Translate(worldCoordinates);
                    transform.position = new Vector3(worldCoordinates.x,worldCoordinates.y);
                    //transform.Translate(touch.position);
                    Debug.Log("Transform 2: " + transform.position);
                }
            }
        }
    }
}

Object now follows my finger. It's kinda slow (staying behind all the time unless I stop) but it's another problem to solve. Thanks a lot for help.

Upvotes: 1

Views: 2305

Answers (2)

Barış &#199;ırıka
Barış &#199;ırıka

Reputation: 1570

touch.position The position of the touch in pixel coordinates.

So first of all, you need to convert position to world (unity world) coordinates.

You should use the Camera.ScreenToWorldPoint to your code(Maybe main camera).

For example:

Vector3 worldCoordinates = yourCamera.ScreenToWorldPoint(fingerEnd);
transform.Translate(worldCoordinates);

Upvotes: 2

Julio Andryanto
Julio Andryanto

Reputation: 82

you need to change

transform.position = fingerEnd;  

to

transform.Translate(fingerEnd);

Upvotes: 1

Related Questions