- Home /
Change Input.GetTouch(0).deltaPosition to object transform position in Unity 4.3 for 2D
Hi, I am new in Unity 3d, I want to move ball on same direction when user tap on ball and move his finger. I got few hints to get user's finger position from "Input.GetTouch(0).deltaPosition;" and get object's position "transform.position" but "transform.position" give me position according to scene view and "Input.GetTouch(0).deltaPosition;" give position according to device screen. How can I convert "Input.GetTouch(0).deltaPosition;" to "transform.position" scene view wise or vice versa, so that it'll be easy for me to move ball, because I am getting confuse with different position. Kindly help me on this and suggest me better approach to accomplish this task. Thanks in advance.
Answer by ArkaneX · Jan 07, 2014 at 01:21 PM
deltaPosition doesn't contain your finger position, but position change between frames.
I am using this code to move position of my object while on tap. Vector2 fingerPosition = Input.GetTouch(0).position; Vector3 worldPosition =camera.ScreenToWorldPoint(new Vector3(fingerPosition.x,fingerPosition.y,0)); transform.position = worldPosition; and app gets crash when I tap on object. I don't know why this happening.
Please post the error you get, but most probably this is related to the fact that if you want to use '`camera`' variable, current game object should have Camera component attached. Does your game object have camera component? If not, you can use Camera.main.ScreenToWorldPoint
(assu$$anonymous$$g your camera is tagged as main camera).
Actually, Its getting crash on debug mode and its not getting crash on device without debugging mode, but position not changing. Just for information I am using raycast to detect object and then change its position according to finger move. I can also change by raycast but I am not doing like this because my task is bit different. I want to throw object as fast as it pulled back. $$anonymous$$indly share with me your experience how to accomplish this task. Thanks :)
You still haven't posted the error you get. Without this info, it's hard to proceed...
Answer by darthtelle · Jan 07, 2014 at 12:50 PM
Try passing the position of your finger touch into Camera.ScreenToWorldPoint(). To get the position of your "mouse" on screen you can using Input.mousePosition. To get a touch event you can add an entry to the Input Manager for the mouse, and use Input.GetButton() to determine whether the button is being held down.
e.g.
if(Input.GetButton("Mouse"))
{
Vector 3 fingerPosition = Input.mousePosition;
Vector3 worldPosition = Camera.ScreenToWorldPoint(fingerPosition);
transform.position = worldPosition;
}
That doesn't make much sense either. deltaPosition contains "delta" information and not a position. It's x and y values represent the change between the last update of the touch device. You might want to use position. $$anonymous$$eep in $$anonymous$$d that ScreenToWorldPoint requires a 3 component position. Since the z value is implicitly 0 it affects the resulting worldposition.
In that case, to get the actual position of the touch on screen, use Input.mousePosition which returns the current mouse position. To get a touch event, use Input.GetButton("$$anonymous$$ouse")
with an entry in the Input$$anonymous$$anager to the mouse. This is then treated as a finger on the screen.
EDIT: Updated my answer to take this into account.
Answer by raimon.massanet · Jan 07, 2014 at 12:47 PM
You should use something like Camera.ScreenToWorldPoint
:
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html
Your answer
Follow this Question
Related Questions
Dragging a 2D sprite with touch 1 Answer
Drag+an+object+to+touch+position(Problem) 0 Answers
Random Movement : 2d 1 Answer
Move object by touch input 5 Answers
A node in a childnode? 1 Answer