This piece of code doesn’t move my cube to the position of my finger. There’s no proper touch input tutorial on internet as well so I’m stuck. I don’t know much about touch inputs. Any kind of help is appreciated.
using UnityEngine;
using System.Collections;
public class CustomTouch : MonoBehaviour {
GameObject cube;
private Ray ray;
private RaycastHit rayHitInfo = new RaycastHit();
private Touch currentTouch;
void Update ()
{
if ( Input.touchCount > 0 )
{
for (int i = 0; i<Input.touchCount; i++)
{
currentTouch = Input.GetTouch(i);
if (Input.GetTouch(i).phase == TouchPhase.Moved)
{
ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
if ( Physics.Raycast(ray, out rayHitInfo))
{
Vector2 v2 = currentTouch.deltaPosition;
Vector3 v3 = new Vector3(v2.x,v2.y,transform.position.z);
cube.transform.position = v3;
}
}
}
}
}
What I would do is start from some working mouse drag code and then modify it for touch. There are a number of drag and drop scripts posted to UA. Here is one I recently posted:
This script only works if the camera is axes aligned. We use the third-party FingerGestures package for our touch input, so I don’t have a lot of depth with the native touch interface, but here is untested conversion to C# and touch of the script.
Note this script will only drag an object if it has the tag ‘Draggable’. You can remove this check (it is in the Raycast() line).
Great. This works better than anything I came up with / found on internet. I will also check out finger gestures package. Thanks for bringing it up as well.
I know that that I can zombiefied this thread, but can you help me on how to convert this script so that if I drag the gameobject it only moves on the x-axis?
I didn’t read it fully, but I think you shouln’t youse deltaPosition (that’s for the position change, such as swiping), instead try Input.GetTouch(i).position.y and Input.GetTouch(i).position.x
edit: I’m also kinda new to developing for touch devices (Android for me), I followed some of this guy’s tutorials and actually helped me a lot: Devin Curry - YouTube
Yeah. I tried that before. It just places my object somewhere out of the screen. I also used this cube.transform.position = Camera.main.ScreenToWorldPoint(v3) It seemed like it didn't have effect at all. I think problem is that "currentTouch.position" is not as accurate as it's supposed to be but I'm not sure.
[untested and from memory] Using one touch is pretty much the same as using one mouse. But, the one difference is that iPad touch positions (and probably all touches) are reported in viewport coords (can print them in a guiText to be sure.) That means they are always from 0-1. Mouse positions are in pixels (screen coords.)
So, the code above should be using ViewportPointToRay (or multiplying touch.position by Screen.width/height, but that’s a pain.) If I’m correct, as it is now, screenPointToRay is always seeing tiny 0-1 numbers, treats them as pixel (0,0) and thinks you are always touching the upper-left(?) corner of the screen.
Hi @Nick4 Here’s my script. It works likely OnMouseDown(), OnMouseDrag(), and OnMouseUp() of course with multi touch support (The RayCast here is 3D):
public GameObject[] touchedObj = new GameObject[5];
void Update () {
Touch[] myTouches = Input.touches;
//Get how much finger/tap in screen
for (int i = 0; i < Input.touchCount; i++) {
Vector3 mainPos = Camera.main.ScreenToWorldPoint (myTouches *.position);*
_ Ray ray = Camera.main.ScreenPointToRay (myTouches .position);_ * RaycastHit hit;* * mainPos.z = -4f;* * if (Physics.Raycast (ray, out hit, 20f)) {* * //OnMouseDown()* _ if (hit.collider != null && myTouches .phase == TouchPhase.Began) { touchedObj = hit.transform.gameObject; * print (“Obj Touched!!”); } } //OnMouseDrag* int ID = myTouches*.fingerId; if (touchedObj [ID] != null){ touchedObj [ID].transform.position = mainPos; print (“Obj Dragged!!”); } //OnMouseUp() if (myTouches.phase == TouchPhase.Ended && touchedObj[ID] != null){ touchedObj [ID].GetComponent ().AddForce (Vector3.right * 50f); touchedObj [ID] = null; print (“Obj Released!!”); } } }*_
@robertbu Hello .. I have some objects moving in a grid. I would be glad if you could help me how I need to write code to move these objects only left, right and forward on the phone.
Hello … I have some objects moving in a grid. I would be glad if you could help me how I need to write code to move these objects only left, right and forward on the phone.
Great. This works better than anything I came up with / found on internet. I will also check out finger gestures package. Thanks for bringing it up as well.
– RioneerI know that that I can zombiefied this thread, but can you help me on how to convert this script so that if I drag the gameobject it only moves on the x-axis?
– ilovecookieee