- Home /
Question by
Iphychoo · Feb 10, 2021 at 01:59 PM ·
vector3mobiletouchtouch controlsscreentoworldpoint
Touch.ScreenPosition doesnt work ?
Hi
I'm trying to basically create a grad shoot mechanic for the ball, im having issues with ScreenToWorldPoint and Touch.ScreenPosition
Half the time the start position works correctly on the screen, half the time it just snaps to 0,0,0 ? as can be seen on the screenshot
void Update()
{
if (Touch.activeFingers.Count == 1)
{
GameTouch(Touch.activeTouches[0]);
}
}
public void GameTouch(Touch touch)
{
Debug.Log(touch.phase);
if (touch.phase == TouchPhase.Began)
{
DragStart(touch.screenPosition);
}
if (touch.phase == TouchPhase.Moved)
{
Dragging(touch.screenPosition);
}
if (touch.phase == TouchPhase.Ended)
{
DragRelease(touch.screenPosition);
}
}
public void DragStart(Vector3 screenPos)
{
dragStartPos = Camera.main.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, 5f));
dragStartPos.z = 0f;
lr.positionCount = 1;
lr.SetPosition(0, dragStartPos);
Debug.Log(dragStartPos);
}
public void Dragging(Vector3 screenPos)
{
draggingPos = Camera.main.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, 5f));
draggingPos.z = 0f;
Vector3 newPos = draggingPos - dragStartPos;
newPos = Vector3.ClampMagnitude(newPos, 1.5f);
newLocation = dragStartPos + newPos;
lr.positionCount = 2;
lr.SetPosition(1, newLocation);
Debug.Log(draggingPos);
}
public void DragRelease(Vector3 screenPos)
{
lr.positionCount = 0;
Vector3 dragReleasePos = Camera.main.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, 5f));
dragReleasePos.z = 0f;
Vector3 force = dragStartPos - dragReleasePos;
Vector3 clampedForce = Vector3.ClampMagnitude(force, maxDrag) * power;
rb2D.AddForce(clampedForce, ForceMode2D.Impulse);
dragStartPos = new Vector3(0,0,0);
}
coding-issue.png
(31.1 kB)
Comment