Aim at touch (2D).

What im trying to achieve is a script that aims the gameobject it’s attached to towards touch position.
I have no problem doing this for standalone with the “lookAt” thingy, And i’ve tried to convert a touch posision to something that works with the lookAt with no succes.

I also tried using a mesh cursor where the gameobject is aimed at the cursor instead of touch, But this brought up some issues since it has to work over a network view.

What i tried was:

using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour {

	void Update ()
	{
		int fingerCount = 0;
		foreach (Touch touch in Input.touches) {
			if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
				fingerCount++;
		if (fingerCount > 0){
		transform.LookAt(Input.GetTouch(0).position);
		}
	}
}
}

But the position of the touch seems to be a little offset. (maybe due to the perspective and the depth position of the touch.)

your problem could be this line:

  transform.LookAt(Input.GetTouch(0).position);

you can solve it with:

  Vector3 worldPoint = Camera.main.ScreenToWorldPoint(touch.position);
  Vector2 touchPos = Vector2(worldPoint .x, worldPoint .y);
  transform.LookAt(touchPos);

this code will convert the screen touch position to the world position that its the one you need so it will solve the problem

super late but im trying to achieve a similar goal to you and have a gameobject look at the users touch location. I cannot find any sources that have really helped but this has been the closest. The issue is though that when i do click on the screen the object looks the opposite way and even changes dimensions depending on the location of the click??!! I have followed your code to the T and cannot replicate the same results.

Hoping someone can help me out here.,super late but im trying to achieve a similar goal to you and have a gameobject look at the users touch location. I cannot find any sources that have really helped but this has been the closest. The issue is though that when i do click on the screen the object looks the opposite way and even changes dimensions depending on the location of the click??!! I have followed your code to the T and cannot replicate the same results.