- Home /
Teleporting player to touch position and ray problem
I have a huge problem which I couldn't resolve for days. When player is tapping the screen 2 times, I want to check if touch hit one of two buttons (left/right arrow), and if not, teleport the player to Input.GetTouch(0).position x and y. I'm a beginner and I also need someone to explain rays and raycasting to me.
I hope for fast response so I can get back to work fast too :(
Answer by GiyomuGames · Jul 10, 2015 at 08:10 AM
To teleport the player you just need to do: transform.position = Input.GetTouch(0).position
For Raycasting: https://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting
But if you are asking these questions I believe you should go through some of Unity's tutorials first. https://unity3d.com/learn/tutorials/modules
Answer by Wested_Crean · Jul 14, 2015 at 02:01 PM
So, I've found the solution on my own.
First, I've created a function that handles the teleportation.
void Teleport ()
{
Vector3 teleTouch = Camera.main.ScreenToWorldPoint(new Vector3 ( Input.GetTouch (0).position.x , Input.GetTouch(0).position.y, 10) );
transform.position = teleTouch;
}
then, in FixedUpdate I've added
if(myBody.velocity.x == 0 && Input.GetTouch (0).phase == TouchPhase.Began)
Teleport();
So function is called only if the player isn't moving (normally he moves by using left/right button)
Works perfectly and I've learned something.
And what if I need to control (move) player without teleporting on touch ??
Answer by Nithinsvs · Jun 11, 2018 at 06:32 AM
And what if i need to control player without teleporting on touch ?? @Wested_Crean
Your answer