Sign in to follow this  
Cuber01

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

Recommended Posts

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);
    }

 

Share this post


Link to post
Share on other sites

just use gameObject.Right

​for example

var bul= instantiate(bullet,gunMuzzle.transform.position,any rotation);
bul.getComponent<rigidbody2d>().addForece(gun.right*200,forcemode.impulse);// if its a 2d game

its a psudo code. remember:

x: right

y:up

z:forward

these are variables that return axis direction of every object

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627715
    • Total Posts
      2978782
  • Similar Content

    • By sprotz
      I like unity3d but the installation size is too big. the package itself is 1GB and the installation is 5GB. What are some good, easy 3d game engines similar to Unity that have smaller file sizes? Excluding engines that lack editors like level editors, world editors etc, with an easy enough scripting language, physics and good documentation.
    • By Canislupus54
       
      I'm looking for a team to help me out with a 3D platformer. Basically, you jump between platforms with parkour elements and you fight enemies with a mix of melee and ranged attacks. This is purely a hobby project. I'm not promising any payment, ever. You can do it for experience, to learn, for fun, whatever, as long as you don't expect to get paid. Right now I need a 3D modeler and animator. Reply or email me at jordestoj@yahoo.com if you're interested. Thanks.
    • By Pixelated_Nate
      Hi all!
      We are looking for a C# programmer for our 2D Action RPG titled Adavia, made in Unity.
      The game itself is akin to Legend of Zelda: Link to the Past, though we're also adding in traditional RPG elements such as Character Creation.
      This is more of a hobby than anything commercial, if it somehow does manage to go commercially, all revenue will be split equally among the team.
      If you're interested, we ask that you be comfortable with:
      Unity Coding A.I's for enemies and NPCs. Working with GUI's. Communicating regularly with the team via Skype (text only). If you have any questions or would like to apply, please contact me at nathan.jenkins1012@gmail.com
       
    • By MrGiratinaORIGINAL

      I'm the co-director of LM Team, a 7-member team currently constructing a fangame based upon the Mega Man series. We've been working for about 2 years now, and both of our current programmers are busy for the next 6 months or so. Because of this, we're currently looking for a programmer (or more) to take care of gameplay and animation programming. We already have a base system in place that may optionally be built upon. This will be a hobby project to invest your free time in, not a gamedev gig.
      We're currently about 67% done with the OST, 75% done with the design work, and 33% done with the graphical work. As such, the specifications of what we need should remain pretty stagnant from now on.
      Here's some links of our team's work, to give you an idea of what we're currently capable of achieving:
      Actual Y+1 Stuff:
      - Current public playlist of music
      - Old footage of the first boss of the game, since been altered slightly based on feedback
      - Mockups
      m9m (director, composer):
       
      - Grand Dad Reboot - Ominous Cavern
      - Lands of Onlup
      - Mega Man Revolution Remix - Wily 2
      MrKyurem (co-director, main designer, me):
      - Boil Man - 10th place in Make a Good Mega Man Level 2 Contest
      NaOH (programmer [not programmer who is currently working on the engine]):
      - Programmer for Make a Good Mega Man Level 2 Contest's release game (did co-op mode and leaderboard functionality)
      - 15 years of programming experience
      - Castlevania 3 controls romhack
      - GML code beautifier
      - Beat-Em-Up engine
      Alm (artist):
      - Cutscene for a personal project
      - Tileset for Boil Man's stage in MaGMML2
      - Concept sprite
      Valo (artist):
      - Jungle Man tileset in Mega Man SD
      - Boxart for Mega Man SD
      - Wily sprite
      Karakato (artist):
      - Robot Masters in BCA
      - Idol Woman
      - Stage Select for separate project
      Deathro (composer):
      - Through the Eye of a Prism - 2nd place in Famicompo 2015's Original category
      - Mega Man SFR - Wily 2
      - Geno Force - Silent Breeze
      Contact
      If you're interested in joining, I'd recommend signing up to Discord, as we almost exclusively communicate through this.
      Discord Tags:
      MrKyurem#8741
      m9m#1104
      Email:
      LMTeam.Y1@gmail.com
    • By ilovegames
      You are the commander of a special forces squadron. You were given a task that appeared simple at first glance - to check for suspicious activity in the building of an abandoned psychiatric hospital. But you could not even imagine what you will actually have to face.
      Download https://falcoware.com/HospitalSurvival.php



  • Popular Now