- Home /
Question by
ramez1998 · Jan 17, 2017 at 10:04 PM ·
move object
my object allways move on the right ,why?,always the object move to the left why??
void Update () { float bar = transform.position.x;
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
Vector3 touchPosition = Input.GetTouch(0).position;
//Check if it is left or right?
if (touchPosition.x > bar)
{
transform.Translate(Vector3.left * 10 * Time.deltaTime);
}
else if (touchPosition.x < bar )
{
transform.Translate(Vector3.right * 10 * Time.deltaTime);
}
}
},
Comment
Answer by tanoshimi · Jan 17, 2017 at 10:07 PM
Because transform.position.x is a value in world space, and Input.GetTouch(0).position.x is a value in screen space.
Use ScreenToWorldPoint to convert your touch position to a world position at a given depth from the camera.