- Home /
Edit this code to move object by touch in x & z
I need a script to move object by touch in x and z axis (on the ground) (Y is air). I have found this code from a site, This is suitable code but there is a problem that can move object in X and Y axis, it means you can drag object to the air and i don't want this. I tried to change it myself but i can't ! Please help me to change this code to move object by touch only in x & z axis. (it means by touch x object moves to right and left and by touch y object moves to forward and backward)
using UnityEngine;
using System.Collections;
public class Drag : MonoBehaviour {
public Transform plane;
private Transform selection = null;
private Vector3 dist;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
foreach(Touch touch in Input.touches)
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
RaycastHit point;
switch (touch.phase)
{
case TouchPhase.Began:
if(Physics.Raycast(ray, out hit, 100))
{
if (hit.rigidbody !=null)
{
selection = hit.rigidbody.transform;
plane.transform.position = hit.point;
}
}
dist = new Vector3(hit.rigidbody.transform.position.x,hit.rigidbody.transform.position.y,hit.rigidbody.transform.position.z) - hit.point ;
plane.collider.enabled = true;
break;
case TouchPhase.Moved:
int layerMask = 1 << 8;
if(Physics.Raycast(ray, out point, 100,layerMask))
{
selection.rigidbody.constraints = RigidbodyConstraints.FreezePosition;
selection.transform.position = point.point + dist;
}
break;
case TouchPhase.Ended:
selection.rigidbody.constraints = RigidbodyConstraints.None;
selection = null;
plane.collider.enabled = false;
break;
}
}
}
}
Comment