- Home /
The question is answered, right answer was accepted
how to offset finger touch ?
Hi : my object falow my finger very well but it teleport where ever i touch and i want it to move the object with offset distance between the object and the finger. and of course the offset distance should be variant if i lifted my finger and touched the screen in different position again. her is my code :
void finger()
{
if(Input.touchCount > 0)
{
Vector3 pl = new Vector3(Input.GetTouch(0).position.x,halfH,10);
transform.position = Camera.main.ScreenToWorldPoint (pl);
}
}
@Ali haten Are you using collider? If yes... All screen has objects with colliders? In other words... Any place that you touch will generate some collision? The Y axis is static or you only want to change the X position like a pong game?
there is no collision with finger it is strange !
Answer by Ali-hatem · Feb 12, 2016 at 01:00 PM
finally problem solved ! by this code and moving the camera 3 points to the right so i don't get negative numbers when i touch the left half of the screen because if camera at zero position and the camera size is 5 then i will get -2.5 to the left and 2.5 to the right. and it's mater because of the offset calculation below and it's hard to explain every thing i had to add texts to the screen the touch position & the transform position & the offset distance to understand every thing but now any one can use this script
if (Input.touchCount > 0)
{
// converting the touch position from ScreenSpace To WorldPoint
Vector3 tpos = Input.GetTouch (0).position;
Vector3 pos = Camera.main.ScreenToWorldPoint (tpos);
// i is a float variable
i = pos.x; // geting only the X position of touch
// setting the offset distance only when touch began
if(Input.GetTouch(0).phase == TouchPhase.Began)
{
// offset is a float variable
offset = transform.position.x - i;
}
if(Input.GetTouch(0).phase == TouchPhase.Moved)
{
transform.position = new Vector2(i+offset,-3);
}
}
}
Follow this Question
Related Questions
touch input error - finger position offset 2 Answers
How I make finger touch move texture offset 0 Answers
How do I offset the scaling center for a cube 1 Answer
gun sway, gun swing, gun offset 0 Answers
Look to the side-Scripting Problem 2 Answers