- Home /
I am new to unity using c# and want to move specific clones to my touch position when I touch them.,Hi I am new to unity and using c#. I want to move clones with touch one at a time wen I touch them.
This script is connected to the prefab I am cloning
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class moveble : MonoBehaviour { Vector3 TP; public static bool CME;
public Transform tr;
private void Start()
{
tr = GetComponent<Transform>();
CME = create.CME;
}
private void Update()
{
if (CME = true && Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
TP = Camera.main.ScreenToWorldPoint(touch.position);
TP.z = 13;
tr.position = TP;
Debug.Log("1");
if (touch.phase == TouchPhase.Ended)
{
}
}
}
},this is the script I am using using System.Collections; using System.Collections.Generic; using UnityEngine;
public class moveble : MonoBehaviour { Vector3 TP; public static bool CME;
public Transform tr;
private void Start()
{
tr = GetComponent<Transform>();
CME = create.CME;
}
private void Update()
{
if (CME = true && Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
TP = Camera.main.ScreenToWorldPoint(touch.position);
TP.z = 13;
tr.position = TP;
Debug.Log("1");
if (touch.phase == TouchPhase.Ended)
{
}
}
}
}
Answer by pihels · Apr 05, 2020 at 01:50 PM
Looks like this script would move all of the objects regardless of which one you are touching? As I understand you want to only move the clone you are touching?
You should specify which clone you want to move by adding a collider to your object and using something like raycasting to find out which object you touched. Another option is adding a collider and using PointerDown and PointerUp events (these should also work with touchscreens).