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

Discussion in 'Game Development (Technical)' started by Cubee01, Feb 21, 2017.

  1. Cubee01

    Cubee01 New Member

    Joined:
    Feb 13, 2017
    Messages:
    5
    Likes Received:
    0
    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 Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer