- Home /
Question by
Christinezhang · May 11, 2020 at 08:17 AM ·
gameobjectraycastunity 2d
why gameobject / raycast when dragged is not accurately following the touch position in unity
so I made a script for multitouch using raycast. when dragged faster, the gameobject will be released, but when moving the gameobject slowly it follows the touch. here the codes
void FixedUpdate()
{
Debug.Log(Input.touchCount);
if (Input.touchCount > 0)
{
touch[Input.touchCount-1] = Input.GetTouch(Input.touchCount-1);
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(touch[Input.touchCount-1].position);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero, Mathf.Infinity);
if (hit.transform != null)
{
if (touch[Input.touchCount - 1].phase == TouchPhase.Moved)
{
hit.transform.position = worldPoint;]
}
else if (touch[Input.touchCount-1].phase == TouchPhase.Began)
{
startPos[touch[Input.touchCount-1].fingerId] = worldPoint;
}
else if (touch[Input.touchCount-1].phase == TouchPhase.Ended)
{
hit.transform.position = startPos[touch[Input.touchCount-1].fingerId];
}
}
}
}
what can i do for drag faster without gameobject will released? sorry for my bad english
Comment