- Home /
Rotate player to LookAt a touch position
Hey everyone, I am currently developing a top down 3D game where I need to rotate my players position in the Y direction to LookAt a touch position. I assume that the problem I am having has to do with changing my touch position into a Vector3 then applying it to the LookAt function. I have searched pretty thoroughly online but have still yet to find an solution to my problem. Any help would be greatly appreciated! Thanks!
void Update() {
if(Input.touchCount > 0)
{
Vector3 touchPos = Input.GetTouch(0).position;
touchPos.z = 0.0f;
player.transform.LookAt(touchPos);
}
}
Answer by MikeNewall · May 21, 2013 at 03:10 AM
Convert the touch position from screen to world space:
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html
@jamesar - when you use ScreenToWorldPoint(), the 'z' parameter is the distance in front of the camera, so you don't want touchPos.z to be 0.0f; The closer the point is to the player, the more extreme the LookAt() rotation will be.
Awesome thanks for the help with this one guys! This lead me to find a fix for the problem I was having. Also, the 'z' parameter must be assigned a value in order for it to work. Cheers!!
Answer by aditya9991 · Jan 14, 2019 at 05:20 PM
Can you share the script @jamesar ? I have similar problem and triled evrything.