I’m trying to plot the coordinates of where my iPhone registered i put down my finger on the screen, so i just “listen” for any touches in the script i attached to the main camera :
GUIText PosText;
if (Input.touchCount > 0)
{
Touch t = Input.touches[0];
if (t.phase == TouchPhase.Began)
{
touchStart = t.position;
}
But the text isn’t plotted on the screen at the same location my finger touches down on the screen, also if i replace
PosText.transform.position = p1; with PosText.transform.position = touchStart; it makes no difference… i don’t get this… What is the easiest way to just record where exactly the screen is touched and print the corresponding coordinates ? I hope someone can help me out, because this seems so easy, but somehow Unity makes this over-complicated in my opinion… Thank you…
Two things, firsty GUIText has a number of values that affect it position i.e. the pixelOffset and anchor. Its a 2D object not a 3D object, andits probably best to leave its position at 0,0,0 and move it around using pixelOffset. In which case you can just do something like:
// Ensure you have set guiText.anchor = TextAnchor.MiddleCenter;
Update () {
if (input.touches.Length > 0)
PosText.transform.position = Input.touches[0].position;
// I'm not going to open Unity to check but you may need to invert the position
// I vaguely remember the screen space being opposite to alignment ... something like:
// var pos:Vector2 = new Vector2(Screen.width - Input.touches[0].position.x, Screen.height - Input.touches[0].position.y);
}
Secondly, for a 3D object, you probably want to consider the z position, from the API docs:
In this case it’s likely a better Z would be the existing Z position.
Hello, i tried your solution… first i just literally used your code but still no text showed up in my screen… Then i just added your code in my original Update() routine… So it should be that the location of touchdown is printed at the start of the line that is drawn on screen:
void Update ()
{
if (Input.touches.Length > 0)
{
Touch t = Input.touches[0];
if (t.phase == TouchPhase.Began)
{
touchStart = t.position;
}
But this doesn’t work, there is no text visible inside the screen… If i change " TextPos.transform.position = pos " to " "TextPos.transform.position = p1 " then something is printed inside the screen but only in the upper left corner and not corresponding to the location of touch on the screen. Finally i tried changing the line :
" Vector2 pos = new Vector2(Screen.width - touchStart.x, Screen.height - touchStart.y); " to Vector2 pos = new Vector2(Screen.width \ touchStart.x, Screen.height \ touchStart.y); and Vector2 pos = new Vector2(Screen.width * touchStart.x, Screen.height * touchStart.y);
and i admit highly unlikely, this didn’t make no difference either.