- Home /
Question by
aSaDaliii · Dec 18, 2014 at 12:47 PM ·
Move character left and right onTouch Unity C#
want to move character left and right, it work as only one side, left or right not both.
if(Input.touches.Length<=0)
{
Debug.Log("no touch");
}
else
{
for(int i=0;i<Input.touchCount;i++)
{
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
if(Input.GetTouch(i).phase==TouchPhase.Began)
{
Debug.Log ("Touch Down");
}
if(Input.GetTouch(i).phase==TouchPhase.Ended)
{
Debug.Log ("Touch up");
}
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
Vector3 touchPosition = Input.GetTouch(0).position;
//Check if it is left or right?
float abc=Screen.width/2;
var xyz=Screen.height;
Debug.Log("Width = "+abc.ToString()+"Height = "+xyz.ToString());
if(touchPosition.x <= abc )
{
Debug.Log ("move left");
this.transform.Translate(Vector3.left * 5 * Time.deltaTime);
}
else if (touchPosition.x >= abc)
{
Debug.Log ("Move right");
transform.Translate(Vector3.right * 5 * Time.deltaTime);
}
}
}
}
Comment
What does it mean "left AND right"? I cannot see how you can move left and right at the same time.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to handle 5 touches at a time? 2 Answers
How to play and stop animation via code in Unity4.3 for 2D objects. 0 Answers
Childbrowser not working with Unity and Phonegap 1 Answer
AdMob iOS for Unity 4.1 2 Answers