Question by
mehdi14 · Dec 24, 2015 at 10:07 PM ·
androidtouch controlstouchscreen
How can convert keyboard controller to touchscreen(android .ios ...etc)
hey guys i m trying for detect touch on anroid and if i playing my games i get this error for console "ArgumentException: Index out of bounds."
this is script for windows :
if (Input.GetKeyDown(KeyCode.Space)||Input.GetMouseButtonDown(0) )
{
GetComponent<AudioSource>().Play();
myRigidBody2d.velocity = new Vector2( myRigidBody2d.velocity.x , jumpheight);
if (myRigidBody2d.velocity.x<0 && myRigidBody2d.velocity.y>0)
{
theAnim.SetFloat("speed" , myRigidBody2d.velocity.y);
theAnim.SetFloat("Xleft",myRigidBody2d.velocity.x);
}
if (myRigidBody2d.velocity.x>0 && myRigidBody2d.velocity.y>0)
{
theAnim.SetFloat("speed" , myRigidBody2d.velocity.y);
theAnim.SetFloat("Xleft",myRigidBody2d.velocity.x);
}
}
else
theAnim.SetFloat("speed" , myRigidBody2d.velocity.y);
and i try for transform this script for android :
if (Input.touchCount == 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
GetComponent<AudioSource>().Play();
myRigidBody2d.velocity = new Vector2( myRigidBody2d.velocity.x , jumpheight);
if (myRigidBody2d.velocity.x<0 && myRigidBody2d.velocity.y>0)
{
theAnim.SetFloat("speed" , myRigidBody2d.velocity.y);
theAnim.SetFloat("Xleft",myRigidBody2d.velocity.x);
}
if (myRigidBody2d.velocity.x>0 && myRigidBody2d.velocity.y>0)
{
theAnim.SetFloat("speed" , myRigidBody2d.velocity.y);
theAnim.SetFloat("Xleft",myRigidBody2d.velocity.x);
}
}
}
else
theAnim.SetFloat("speed" , myRigidBody2d.velocity.y);
Comment
Best Answer
Answer by mehdi14 · Dec 25, 2015 at 09:45 PM
if (Input.GetKeyDown(KeyCode.Space)||Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began))
{
GetComponent<AudioSource>().Play();
myRigidBody2d.velocity = new Vector2( myRigidBody2d.velocity.x , jumpheight);
if (myRigidBody2d.velocity.x<0 && myRigidBody2d.velocity.y>0)
{
theAnim.SetFloat("speed" , myRigidBody2d.velocity.y);
theAnim.SetFloat("Xleft",myRigidBody2d.velocity.x);
}
if (myRigidBody2d.velocity.x>0 && myRigidBody2d.velocity.y>0)
{
theAnim.SetFloat("speed" , myRigidBody2d.velocity.y);
theAnim.SetFloat("Xleft",myRigidBody2d.velocity.x);
}
}
else
theAnim.SetFloat("speed" , myRigidBody2d.velocity.y);
timeHit -= Time.deltaTime;
if (timeHit <= 0f)
timeHit =0f;