Sign in to follow this  
Followers 0
Cuber01

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

1 post in this topic

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

 

0

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

0

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  
Followers 0

  • Similar Content

    • By khawk
      The GitHub for Unity extension announced by GitHub at GDC 2017 is now open source. Developers can now download the extension and use Git and GitHub without leaving Unity.
      The extension includes support for Git LFS, file locking, and the GitHub workflow from Unity.

      Learn more from the blog announcement at  https://github.com/blog/2385-github-for-unity-is-now-open-source.
       

      View full story
    • By khawk
      The GitHub for Unity extension announced by GitHub at GDC 2017 is now open source. Developers can now download the extension and use Git and GitHub without leaving Unity.
      The extension includes support for Git LFS, file locking, and the GitHub workflow from Unity.

      Learn more from the blog announcement at  https://github.com/blog/2385-github-for-unity-is-now-open-source.
       
    • By Altri
      Arena Of Glory
      a mobile moba game
      The Idea
      There's a lot of mobas out in the market right now. On PC at least, most don't do any good. The big 3 (LoL, Smite, Dota2) outshine any new moba coming out, but it's a different story when it comes to mobile gaming. The only quality moba game on mobile at the time of writing this is Vainglory, a 3v3 battle arena game "perfected for touch". The rest are cheap league of legends clones some of which literally use stolen models and sound effects. I'd like to create a better moba on mobile phones. Something that you can truly play anywhere without having to bring your iPad to play comfortably like Vainglory. A moba game truly perfected for touch with a 5v5 battle mode.
      The Team
      Currently there's 2 people in the team, possibly 3 (talking to possible concept artist as I write this). We're both programmers. It's always been our goal to make games. After some school and more self teaching, we have finally made it to a point where we can write games.  Of course, you might see right away how this is an issue. We can't do art. I mean I have tried but I can't do anything digitally.
      Current Progress
      Well, we have been working on the game for a 1 and 3-4 months. We have most of the core features done and even have temp maps put in place to test along with cheap 3d models. The game is working, only that in it's current state it can only do 3v3 battles. (Originally we were going to keep it 3v3 but later changed our minds.) We have a poorly made UI that is not appealing in any way, but can get the job done as we continue. 
      We have: Nickname choosing , friends list, tutorial for beginners,  gold: currency gained through playing and premium currency (in app purchase) to buy skins, etc. Hero list, 3v3 battle mode.. we have most of the core done.
      What's left: Implement 5v5 battles, World Chat, User Registration check (so that you never lose your account), new UI/UX/HUD & New artwork (new hero concepts, 3d models, logo, etc). 
      What we Need
      I realize this is a big project. We need people who are passionate about game design/development. People who can stick around because they love the project or just love game making in general. There's a huge opportunity for us in the mobile market and we want to offer revenue share and equity to artists and programmers that would want to jump on board. Stick through and you'll see that your time will not be wasted. 
      How to contact
      I guess you can PM me here or add me on Discord @ Altri#7510.
      You may also email me @ plus5interactive@gmail.com.
       
      Thank you so much for your time,
      Altri
       
      [SCREENSHOT ADDED]
      Keep in mind we don't plan to use any of our current assets. We want everything made organically by our team and have our own unique style.
       

    • By Gabriel_Solon
       Hi, this is a fighting project that im making for a month now.
       
       
      The game mechanics work like this:
       - There are 3 stances, low, mid and high, each one starts a combo, it also blocks any attack coming from the same stance.
       - Special Attacks are unblockable unless a Special Defense (dashes or parries) is engaged       -(Not yet properly implemented)
       - Special moves have a 5 second cooldown
       - I intend on having 3 characters with distinct weapons and fighting styles based on Historical/Mithical personalities
       
       Tell me what do you think
    • By NeroGame
      hey everyone!
      so im trying to create an in game shope to spend the in game currency and i want the kind of shop that when you touch the skin you unlock it and select it (i've tried several ways didnt work only with a button ) so can anyone please help
  • Popular Now