- Home /
move a specific object with finger in unity iphone.
Hello guys, i am trying hard to move an object on plane with my finger. but my code detect the touch of full iphone screen not just that object. can anybody please tell me how i can get the touch of that paticular object and move it on the screen with my finger.
my code is this.
var speed : float = 5;
function Update () {
for(var touch : Touch in Input.touches) { if(touch.phase == TouchPhase.Began) { Debug.Log("Finger has been touched...");
}
if(touch.phase == TouchPhase.Moved) {
Debug.Log("Finger is Moving...");
transform.position.x += Time.deltaTime * speed;
}
}
}
Answer by Ronald Meijers · Apr 29, 2011 at 11:39 AM
Hey,
you need to use the ScreenPointToRay to determine if you are touching an object. Thus:
for (var touch : Touch in Input.touches) { var ray = Camera.main.ScreenPointToRay (touch.position); var hit : RaycastHit;
if(touch.phase == TouchPhase.Began && Physics.Raycast(ray, hit)) { Debug.Log("Finger has been touched...");
}
if(touch.phase == TouchPhase.Moved && Physics.Raycast(ray, hit)) { Debug.Log("Finger is Moving...");
transform.position.x += Time.deltaTime * speed;
}
Now you can check if the object the ray has hit is actually the object you need by adding:
if(hit.collider.gameObject.tag == "Your Object"){
}
Hope this helped!
Your answer
Follow this Question
Related Questions
move a specific object with finger in unity iphone. 0 Answers
How do I add gravity to an object? 1 Answer
Light Baking Problem 0 Answers
How to get A Camera Collision? 1 Answer