我想在Y轴上同时拖动两个游戏对象,所以我编写了这段代码,但实际上,它只是要拖动一个游戏对象
我已经测试了很多方法来做到这一点,但是我做不到
你们能帮助我做到这一点吗?
非常感谢
这是我的代码:
using UnityEngine;
using System.Collections;
public class Drag2 : MonoBehaviour
{
private float dist;
private bool dragging = false;
private Vector3 offset;
private Transform toDrag;
void Update()
{
Vector3 v3;
if (Input.touchCount < 1)
{
dragging = false;
return;
}
Touch[] touch = Input.touches;
for (int i = 0; i < Input.touchCount; i++)
{
Vector3 pos = touch[i].position;
if (touch[i].phase == TouchPhase.Stationary)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(pos);
if (Physics.Raycast(ray, out hit) && (hit.collider.CompareTag("Object")))
{
Debug.Log(Input.touchCount);
toDrag = hit.transform;
dist = hit.transform.position.z - Camera.main.transform.position.z;
v3 = new Vector3(0, pos.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.position - v3;
dragging = true;
}
}
if (dragging && touch[i].phase == TouchPhase.Moved)
{
v3 = new Vector3(0, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
toDrag.position = v3 + offset;
}
if (dragging && (touch[i].phase == TouchPhase.Ended || touch[i].phase == TouchPhase.Canceled))
{
dragging = false;
}
}
}
}