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

3 posts (showing 1-3)

Market Level 0Community Level 0
2 posts


 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,;

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

            if (enemy != null)



                //Debug.Log("We" + + "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);




                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




posted 2017-04-16T15:06:10-07:00

FGL AdminCommunity Level 4
321 posts

In this case, I would pass the firing angle to the bullet when it is created rather than worrying about raycasts and other complicated methods. A good principle to try to follow is to keep your game logic separate from your graphics where possible - Unity makes this line easy to go over, but it will simplify your life if you do so. The idea would be that you keep track of the gun angle yourself, rather than relying on Unity's game object rotation etc, then set the rotation of the unity object based on your defined angle. That way, you will always have a variable that gives the exact angle to shoot at - no need to figure it out from Unity's methods - and if you change how you alter the angle later, you only need to change your own game's logic - you don't need to mess with the Unity/graphics 'layer'.

The second part is actually making the bullet move and rotate to the desired angle. Rotation is easy - just set the rotation of the Bullet's transform to your angle variable. Moving is slightly tricker, but not much -- you could use something like this ( or you could use trigonometry:

(each frame) x += cos(angle) * distance; y -= sin(angle) * distance

Hope that helps put you on the right path!

posted 2017-04-16T15:35:45-07:00 | edited 2017-04-16T15:36:05-07:00

Market Level 0Community Level 0
2 posts

Thanks for the reply I really appreciate it!! I undertand what you are saying excpet that I am still pretty new to unity and c#. I I dont really understand how to add to my game. some tips on how to do that would be grealty appreciated! Thanks!

posted 2017-04-16T15:43:09-07:00