Jump to content
  • Advertisement
Sign in to follow this  
Cuber01

Unity How do I get a bullet to shoot to a mobile joystick position in Unity?

This topic is 582 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a 2D platformer and I am adding mobile controls. I am trying to get the player to shoot when you touch and move the joystick.

I tried to change all of the Input to CrossPlatformInputManger but when I use the joystick nothing happens.

Please any sugggestions will be helpful! Thanks!!!!

Here is what is in the update method

 

void Update () {

        if (fireRate == 0)

        {

            if (CrossPlatformInputManager.GetButtonDown("Fire1"))     

            {

                Shoot();          

            }

        }

        else

        {

            if (CrossPlatformInputManager.GetButton ("Fire1") && Time.time > timeToFire) 

            {

                timeToFire = Time.time + 1 / fireRate;

                Shoot();

            }

        }

    }

 

and the Shoot Method

 

void Shoot ()

    {

        Vector2 joyStickPosition = new Vector2(Camera.main.ScreenToWorldPoint(CrossPlatformInputManager.mousePosition).x, Camera.main.ScreenToWorldPoint(CrossPlatformInputManager.mousePosition).y);

        Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);

        RaycastHit2D hit = Physics2D.Raycast (firePointPosition, joyStickPosition-firePointPosition, 100, whatToHit);

 

        Debug.DrawLine(firePointPosition, (joyStickPosition - firePointPosition)*100, Color.cyan); 

        if (hit.collider != null)

        {

            Debug.DrawLine(firePointPosition, hit.point, Color.red);

            Enemy enemy = hit.collider.GetComponent<Enemy>();

            if (enemy != null)

            {

                enemy.DamageEnemy(Damage);

                //Debug.Log("We hit " + hit.collider.name + "and did " + Damage + "damage");

            }

        }

I am working on a 2D platformer and I am adding mobile controls. I am trying to get the player to shoot when you touch and move the joystick.

I tried to change all of the Input to CrossPlatformInputManger but when I use the joystick nothing happens.

Please any sugggestions will be helpful! Thanks!!!!

Here is what is in the update method

void Update () {
    if (fireRate == 0)
    {
        if (CrossPlatformInputManager.GetButtonDown("Fire1"))     
        {
            Shoot();          
        }
    }
    else
    {
        if (CrossPlatformInputManager.GetButton ("Fire1") && Time.time > timeToFire) 
        {
            timeToFire = Time.time + 1 / fireRate;
            Shoot();
        }
    }
}

And the Shoot method

void Shoot ()
{
    Vector2 joyStickPosition = new Vector2(Camera.main.ScreenToWorldPoint(CrossPlatformInputManager.mousePosition).x, Camera.main.ScreenToWorldPoint(CrossPlatformInputManager.mousePosition).y);
    Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
    RaycastHit2D hit = Physics2D.Raycast (firePointPosition, joyStickPosition-firePointPosition, 100, whatToHit);

    Debug.DrawLine(firePointPosition, (joyStickPosition - firePointPosition)*100, Color.cyan); 
    if (hit.collider != null)
    {
        Debug.DrawLine(firePointPosition, hit.point, Color.red);
        Enemy enemy = hit.collider.GetComponent<Enemy>();
        if (enemy != null)
        {
            enemy.DamageEnemy(Damage);
            //Debug.Log("We hit " + hit.collider.name + "and did " + Damage + "damage");
        }
   

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!