# How can I shoot when I touch the Box Collider

I want to change the Computer Input to Mobile Touch Input, if I press Space Button it shoots but how do I change this [Code] to Touch Input ?

I want it to have it into void OnTouchDown() but what do I have to put into the if(...) in the ShootingScript if I want to have it for Mobile to work ?

Here is my Shooting Script and my Player Controller Script

ShootingScript:

public GameObject shootingBullets;

// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
Instantiate(shootingBullets, transform.position, Quaternion.identity);
}
}


PlayerControllerScript:

public float jumpHeight;

void OnTouchDown()
{
if (GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>().velocity.y < 0.001f && GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>().velocity.y > -0.001f)
{
}

}

// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.UpArrow) && GetComponent<Rigidbody2D>().velocity.y < 0.001f && GetComponent<Rigidbody2D>().velocity.y > -0.001f)
{
}

if (Input.GetKeyDown (KeyCode.DownArrow))
{
}

}


So from what I understand you want to shoot using OnTouchDown(), well if thats what you want to do then you simply repeat what you did in the playerController class.

ShootingScript:

public GameObject shootingBullets;

// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
Instantiate(shootingBullets, transform.position, Quaternion.identity);
}
}

void OnTouchDown()
{
//no if needed as it is only called when touch input is down
Instantiate(shootingBullets, transform.position, Quaternion.identity);
}


alternatively you could put it into the update function using the input class:

void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
Instantiate(shootingBullets, transform.position, Quaternion.identity);
}
else if(Input.touches[0].deltaPosition.y < 0.01f){
//if the first touch on the screen is moving down start shooting
Instantiate(shootingBullets, transform.position, Quaternion.identity);
}
}


With the that last bit of code I'm assuming that the OnTouchDown() is literally when the player is moving there finger down the screen. If it was just touching the screen replace that with:

else if(Input.touchCount > 0)
//if the first touch on the screen is moving down start shooting
Instantiate(shootingBullets, transform.position, Quaternion.identity);
}


I think that solves your problem unless I've misunderstood your question :)

• On your first Code ther happens nothing, I"ve done a Debug and it is touching if I press at it but it doesnt shoot why ever.. On your Second Code I get an Error it says the Array is out of the Range. But I fixed it, the Code with just OnTouchDown() was correct. The bullets spawned in the Center of the Box Collider that I pressed, I just moved the Box Collider into the Player as child and now its spawns exactly from the Player, thanks for your help ! May 8 '16 at 10:10