- Home /
How to convert touch position to canvas position?
I've tried many things, with little to no success. My main goal is to convert a touch position into canvas position. Any help you could provide is much appreciated.
Answer by Benjames · Mar 14, 2020 at 11:08 AM
No reply WTF!
I have this so far but it doesnt scale correctly
Vector2 screen = new Vector2(Screen.width, Screen.height);
Vector3 ConvertTouchToCameraUIPoint(Vector3 position) {
Vector3 pos = Camera.main.ScreenToViewportPoint(position) - new Vector3(.5f, .5f, 0);
pos = pos * screen * .025f;
return(new Vector3(pos.x, pos.y, 0));
}
Answer by MoreDragons · Jul 13, 2021 at 06:09 AM
I know this is a little old but man did this take some googling so sharing my solution here for future coders. hope this helps
var touchpoint = Instantiate(Resources.Load("prefabs/touchpoint"), new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
//set it parent to the main canvas
touchpoint.transform.parent = MainCanvas.transform;
//convert touch position to localposition reletive to canvas
Vector2 localPos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(MainCanvasRect, new Vector3(t.position.x, t.position.y, 0), null, out localPos);
//set position on canvas
touchpoint.transform.localPosition = localPos;
Your answer
Follow this Question
Related Questions
Cross Platform Input Controller Not Working on Mobile Device 2 Answers
I can't use more that two buttons at the time. How do I change that? 0 Answers
How can I rotate a camera useing 2 touches? 0 Answers
Place for touch and click command. 1 Answer
New input system doesn't work on webgl build for android 0 Answers