Search Unity

How to get a bullet to shoot in the direction of the gun on mobile.

Discussion in 'iOS and tvOS' started by cuber01, Apr 16, 2017.

  1. cuber01

    cuber01

    Joined:
    Sep 16, 2016
    Posts:
    21
    Hello

    I am working in unity with c#. I am working on a 2D platformer for mobile. The way I currently have it setup is one joystick that moves the charatcer makes the charatcer arm rotate and make the charatcer shoot the gun when pressed. Which all workd fine. But the problem I am having is the bullets from the gun are not going in the correct direction. Currently the bullets go to the rigtht no matter where I point the gun. I have tried in the raycats to use transform.right and transform.forward. All end up with the same result. the firepoint for the gun is an empty game object and the firepoint in the script is an tranfrom. I tried changing it to an GameObject in the script but I got the same results. If someone could please help it would be grealty appreciated!!!

    Here is the shoot function with the raycast.


    public void Shoot ()
    {
    Vector3 firePointPosition = new Vector3 (firePoint.position.x, firePoint.position.y, 0f);
    RaycastHit2D hit = Physics2D.Raycast (firePointPosition, firePoint.transform.right, 100, whatToHit, 0f);

    Debug.DrawRay(firePointPosition, firePoint.transform.right * 100);
    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;
    }
    }

    And the effect method


    void Effect (Vector3 hitPos, Vector3 hitNormal)
    {
    Transform trail = Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation) as Transform;
    LineRenderer Lr = trail.GetComponent<LineRenderer>();

    if (Lr != null)
    {
    Lr.SetPosition(0, firePoint.position);
    Lr.SetPosition(1, hitPos);

    }

    Destroy(trail.gameObject, 0.04f);

    if (hitNormal != new Vector3(9999,9999,9999))
    {
    Transform hitParticle = Instantiate(HitPrefab, hitPos, Quaternion.FromToRotation (Vector3.right, hitNormal))as Transform;
    Destroy(hitParticle.gameObject, 1f);
    }

    Transform clone = Instantiate (MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
    clone.parent = firePoint;
    float size = Random.Range(0.6f, 0.9f);
    clone.localScale = new Vector3 (size, size, size);
    Destroy(clone.gameObject, 0.02f);

    //Shake the Camera
    camShake.Shake(camShakeAmt, camShakeLength);

    //Play shoot sound
    audioManger.PlaySound(weaponShootSound);
    }
     
  2. bitjunk

    bitjunk

    Joined:
    May 19, 2017
    Posts:
    16
    I had similar issues as you descripted. The key to figure this out is to determine the angle from your origin point to your destination point. Do not over think this to much. Check out this website.

    Using Trigonometry to Animate Bounces, Draw Clocks, and Point Cannons at a Target
    https://inventwithpython.com/blog/2...es-draw-clocks-and-point-cannons-at-a-target/

    I know this is not for C# and Unity but the same principals apply to getting your bullets to fly in the correct direction. Unity provides you with the same math functions. Get very familiar with the difference between radians and degrees. Create a library for yourself of functions do the shooting math. You can then use that library again in the next game and you will be that much further ahead. This is what I'm actually doing right now.

    If your creating a 2D shooter another interesting trick is to Angle the bullet in the direction your shooting. You can does this with this line of code.

    transform.rotation = Quaternion.Euler(0, 0, angleInDegrees);

    The transform equals your bullet. I also found that if you don't want your bullet to slowly fall to the ground as it's traveling to its destination point. You also have to recalculate its Y position as it travels in the X direction. Updating the bullets transform position frame by frame to keep it on course. You can do this fairly easy once you know your bullets direction angle. All you basically have to think about is that the X direction is the distance form the origin to the outside edge of a circle or the radius. With the radius/distance and the angle you can recalculate the Y position.

    Lastly a Raycast is not need in my description. Because the bullet flying to it's destination is a visual version of the Raycast in a sense. You can use the bullets collider to detect if it hit something and go from there. So for example if the bullet hits the ground. It's travel cycle ends and is then destroyed. If the bullet hits a enemy the bullet is again destroyed but now the enemy dies or explodes or something.

    Hopefully this was helpful.