Make a cube follow your finger iOS C#

*Tired Sob…

Anyone know how or where i can find some code to attach to a Cube and make it follow my finger for iOS in C#?

I’ve trolled the whole interwebz and can’t seem to find any ideas as to how to go about this, im not asking for code (but it would be nice) rather im looking some leads as to where i can learn about it.

Any ideas?

thanks guys

There are methods that allow you to transform a screen point into a world point and such.

Camera.main.ScreenToWorldPoint for example. You can use the touch input as screen coordinates and set the objects position accordingly. You may have to fiddle around with the depth as the screen coordinates are only 2-dimensional, but you didn’t provide any information about that so it’s difficult to tell you any further information about how to approach that.

OMG That took way too long to work out!

I’ve still got issues though, I’m trying to attach it to a cube in the scene, though cant work out the syntax. Any ideas?

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

public float speed = 4;
private GameObject theCube;


void Start(){
	theCube = GameObject.Find("Cube");
}

    void Update() {
        int fingerCount = 0;
        foreach (Touch touch in Input.touches) {
        	float step = speed * Time.deltaTime;
            if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                
                print(Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x,touch.position.y,3f)));

                theCube.transform.position = Vector3(theCube.transform.position, (Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x,touch.position.y,3f))), step);
            
        }

        
    }
}