Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Muki · Mar 29, 2017 at 11:55 PM · unity 2d

Need help getting bullets to go the right direction is a 2D enviroment

I have this script to shoot bullets towards the mouse cursor but it always shoots it at a 45? degree angle. Heres the script just for the shooting

I have a system to create a bullet and shoot it towards the mouse cursor but I also have an arm that its rotating around.

void pistolShooting() { Vector3 aimPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); aimPos.z = 0;

     //Creating the bullet and shooting it
     var pel = Instantiate(Bullet, pistolBulletSpawn.position, pistolBulletSpawn.rotation);
     pel.GetComponent<Rigidbody2D>().AddForce(aimPos.normalized * 8000f);
     //Playing the bullet noise
     //Shot.Play();
     //shooting and reloading
     usingBulletPerMag -= 1;

 }


[1]: /storage/temp/90928-screenshot-82.png

screenshot-82.png (213.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Mar 30, 2017 at 01:34 AM

script is Untested but this is the idea:

         Vector3 mp;
         RaycastHit hit;
         // get mouse position on screen with raycast
         // mouse must be on top of object
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
         if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit)) {
 
                 
                         //subtract to get direction
                         mp = hit.point - pistolBulletSpawn.position;
                         rigidbody.AddForce (mp.normalized * 8000f);
                 }

 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Muki · Apr 01, 2017 at 06:21 PM 0
Share

Where would I place the instantiate because if its inside the physics raycast statement, it never spawns.

avatar image toddisarockstar Muki · Apr 02, 2017 at 02:54 PM 0
Share

actually yes, you would put it inside the raycast! and The raycast would need to be inside of a statement that gets the mouse click/button.

this works as long as the mouse click is on top of a collider. if you are unsure put a print statement inside the raycast brackets and another print statement right outside the brackets to locate where the problem is. $$anonymous$$aybe you are not getting the correct rigidbody. or maybe you are not getting the click.

you would need to change my line about the rigidbody to get the correct one. this was just a quick example to show you need a raycast to convert the mouse position to a real location.

avatar image Muki toddisarockstar · Apr 03, 2017 at 03:40 AM 0
Share

heres what I had and im not sure if its wrong or anything

void pistolShooting() {

     if (pistolShootable)
     {
         Vector3 mp;
         RaycastHit hit;
         // get mouse position on screen with raycast
         // mouse must be on top of object
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
         {
             var pel = Instantiate(Bullet, pistolBulletSpawn.position, pistolBulletSpawn.rotation);

             //subtract to get direction
             mp = hit.point - pistolBulletSpawn.position;
             pel.GetComponent<Rigidbody2D>().AddForce(mp.normalized * 8000f);
             StartCoroutine(pistolFix());
         }
     }
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PUN 2, Player A not seeing Player B's hat 0 Answers

Unity 2D - Pivot points based on slice, not on original texture. 0 Answers

Camera Effects for 2d 0 Answers

Unity 4.3, generate 2d mesh 2 Answers

2D C# Jump Script Addforce 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges