- Home /
Question by
Saepirist · May 04, 2015 at 10:27 PM ·
inputscreenaccelerationwidth
Input.acceleration with Screen.width
I want my player object to go right if player hold his finger at the right half of the screen and same with left. What should i write in to script? Is something like this would work?
If(Input.acceleration.x > (Screen.widht / 2))
moveHorizontal = new Vector3(0.0f, 0.0f, 1.0f)
If(Input.acceleration.x < (Screen.widht / 2))
moveHorizontal = new Vector3(0.0f, 0.0f, -1.0f)
Comment
maybe If(Input.mousePosition.x > (Screen.widht / 2)) or you want with Input.acceleration?
Answer by hbalint1 · May 04, 2015 at 10:34 PM
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.began)
{
Touch touch = Input.GetTouch(0);
if (touch.position.x <= Screen.width / 2f) // if left side of screen is touched
{
GetComponent<Rigidbody>().AddForce(Vector3.left * moveSpeed);
// you should declare moveSpeed as a float. Like: public float moveSpeed = 3.0f;
}
else if (touch.position.x > Screen.width / 2f) // if right side touched
{
GetComponent<Rigidbody>().AddForce(Vector3.right * moveSpeed);
}
}
Your answer
Follow this Question
Related Questions
Screen width and height, how to bypass?! 1 Answer
How to calibrate accelerometer according to start ? 1 Answer
Help with android shake intensity. 0 Answers
Object resolution 0 Answers