Что я хочу сделать, так это когда пользователь прикасается к объекту, он может его перемещать, и особенно с помощью мультитач-пальца на экране.
Я тоже пытался сделать этот луч щелчком мыши, но у меня все та же проблема.
для этого я использую этот код при обновлении
public LayerMask touchInputMask;
private static List<GameObject> touchList = new List<GameObject>();
public static Dictionary<int, objectst> touchobjects = new
Dictionary<int, objectst>();
private GameObject[] touchesOld;
private RaycastHit hit;
public GUIText Count, IndexLift;
private Vector3 targetPos;
public struct objectst { public Vector3 screenPoint; public Vector3 offset;
}
void Update(){
if (nbTouches > 0)
{
//nbTouches = 5;
//print(nbTouches + " touch(es) detected");
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
for (int i = 0; i < nbTouches; i++)
{
Touch touch = Input.GetTouch(i);
//print("Touch index " + touch.fingerId + " detected at
position " + touch.position);
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out hit,Mathf.Infinity,
touchInputMask))
{
GameObject recipient = hit.transform.gameObject;
print("#### touch hit name object "+recipient.name);
touchList.Add(recipient);
//recipient.;
if (touch.phase == TouchPhase.Began)
{
print("#### tcouh begin");
objectst tempobj = new objectst();
tempobj.screenPoint =
Camera.main.WorldToScreenPoint(recipient.transform.position);
tempobj.offset = recipient.transform.position -
Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x,
touch.position.y, tempobj.screenPoint.z));
touchobjects.Add(touch.fingerId, tempobj);
}
if (touch.phase == TouchPhase.Stationary || touch.phase
== TouchPhase.Moved)
{
print("#### tcouh stationary or moved");
Vector3 curScreenPoint = new Vector3(touch.position.x, touch.position.y,
touchobjects[touch.fingerId].screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + touchobjects[touch.fingerId].offset;
print("#### objet doit être deplacer x = "+curPosition.x+"y = "+curPosition.y);
recipient.transform.position = curPosition;
}
if (touch.phase == TouchPhase.Ended)
{
print("#### tcouh ended");
Vector3 curScreenPoint = new Vector3(touch.position.x, touch.position.y,
touchobjects[touch.fingerId].screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) - touchobjects[touch.fingerId].offset;
recipient.transform.position = curPosition;
}
}
}
}
}
ИЗМЕНИТЬ
снимок экрана
for
? Поместите Debug.Log туда, чтобы проверить - person Programmer   schedule 17.01.2018physics.raycast
, поэтому ваш коллайдер должен быть 3D-коллайдером, чтобы сделать raycast против 2D-коллайдера, вам нужно использоватьphysics2d.raycast
- person AresCaelum   schedule 17.01.2018