How to get a bullet to fire in the direction of the gun

Discussion in 'Game Development (Technical)' started by Cubee01, Apr 7, 2017.

  1. Cubee01

    Cubee01 New Member

    Joined:
    Feb 13, 2017
    Messages:
    7
    Likes Received:
    0
    Hello

    I am working on a 2D platformer. I have a charatcer that his arm rotates and there is a gun he is holding. I am making this game for mobile and I have the arm rotation and the gun shooting on a joystick. But when the gun fires the bullets do not go in the direction of the gun. Like when the gun is facing to the right the bullets go straight up. I Would like it for the bullets to go in the same direction as the gun. Some help would be grealty appreicated!!

    Here is the shoot function I have in c#
    public void Shoot ()
    {
    Vector3 firePointPosition = new Vector3 (firePoint.position.x, firePoint.position.y, 0f);
    RaycastHit2D hit = Physics2D.Raycast (firePointPosition, firePoint.right, 100, whatToHit, 0f);

    Debug.DrawLine(firePointPosition, (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");
    }
    }

    if (Time.time >= timeToSpawnEffect)
    {
    Vector3 hitPos;
    Vector3 hitNormal;

    if (hit.collider == null)
    {
    hitPos = (firePointPosition) * 30;
    hitNormal = new Vector3(9999, 9999, 9999);
    }
    else
    {
    hitPos = hit.point;
    hitNormal = hit.normal;
    }



    Effect(hitPos, hitNormal);
    timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
    }
    }
     

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