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 RouniXd · Nov 07, 2018 at 07:29 PM · shootingmouse position

How can I shoot a projectile into mouse position?

Hello. I run into a problem becouse i dont know how to shoot bullet at mouse direction. Thats my code so far:`

 void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             float mouseX;
             float mouseY;
             mouseX = Input.mousePosition.x;
             int intMouseX = (int)mouseX;
             mouseY = Input.mousePosition.y;
             int intMouseY = (int)mouseY;
 
             Vector2 FinalMousePos;
             FinalMousePos.x = intMouseX;
             FinalMousePos.y = intMouseY;
 
             Instantiate(bullet, Gracz1.transform.position, Gracz1.transform.rotation);
             bullet.GetComponent<Rigidbody2D>().velocity = FinalMousePos * Bulletspeed;
         }
 
     }

Thanks for help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RouniXd · Nov 08, 2018 at 07:41 PM

Ok i completely revamped my entire script. Now it looks like this:

 if (Input.GetButtonDown("Fire1"))
         {
                 clickPos = Vector3.one;
                 Vector3 punktA = new Vector3(1, 2, 0);
                 Vector3 punktB = new Vector3(2, 1, 0);
                 Vector3 punktC = new Vector3(3, 3, 0);
                 Plane plane = new Plane(punktA, punktB, punktC);
                 Ray ray = cam.ScreenPointToRay(Input.mousePosition);
                 float DistanceToPlane;
                 if (plane.Raycast(ray, out DistanceToPlane))
                 {
                     clickPos = ray.GetPoint(DistanceToPlane);
                 }
                 Vector3 finalClickPos = new Vector3(clickPos.x, clickPos.y, 1);
                 Instantiate(bullet, transform.position, Quaternion.LookRotation(finalClickPos));
 
         }


It works ok but when i instentiate bullet it gets rotated to the opposite corner (for instance when i click in the upper right my bullet gets rotated towards upper left corner) How can i fix this? @RustyCrow

Comment
Add comment · Show 1 · 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 RustyCrow · Nov 08, 2018 at 08:24 PM 0
Share

$$anonymous$$ark this thread as Answered and create a new thread with your new question. But i am sure you will find a answer if you keep trying.

avatar image
0

Answer by RustyCrow · Nov 07, 2018 at 08:04 PM

Try ->

 var mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 bullet.GetComponent<Rigidbody2D>().velocity = (Character.position - mousePosition).normalized * bulletSpeed

Edit :
Things in Arrow brakets dosent show up. so i added it in as code. Also added how to get mouse world position since OP did not do it correctly.

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 RouniXd · Nov 07, 2018 at 08:32 PM 0
Share

Ok. Firstly, when i delete rigidbody right next to getcomponent i have an error, so i have to keep rigidbody. Secondly, this does not work, but i spotted something interesting. When i write in debuglog Gracz1.transform.position - Final$$anonymous$$ousePos i always get a negative value, whetever i click on left or right side of my character. But, when i type in debuglog only Final$$anonymous$$ousePos, i get excact same value, but positive. I think that my script does not detect mouse position correctly(?). How can i fix this? Anyway, thats my new code:

   Vector3 Final$$anonymous$$ousePos;
                 Final$$anonymous$$ousePos.x = int$$anonymous$$ouseX;
                 Final$$anonymous$$ousePos.y = int$$anonymous$$ouseY;
                 Final$$anonymous$$ousePos.z = 0;
     
                 Instantiate(bullet, Gracz1.transform.position, Gracz1.transform.rotation);
                 bullet.GetComponent<Rigidbody2D>().velocity = (Gracz1.transform.position - Final$$anonymous$$ousePos).normalized * Bulletspeed;
                 Debug.Log(Gracz1.transform.position - Final$$anonymous$$ousePos);
                 Debug.Log(Final$$anonymous$$ousePos);
 
avatar image RustyCrow RouniXd · Nov 08, 2018 at 02:18 PM 0
Share

Yes i think you are correct, how are you getting your mouse mouse coords? Try this var Final$$anonymous$$ousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

avatar image RouniXd RustyCrow · Nov 08, 2018 at 07:42 PM 0
Share

Check out my reply, i accidentaly posted it as answer, not comment

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

97 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 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

2D Platformer – changing the cursor position when facing left or right 0 Answers

Unity 2D - Bullets don't follow mouse 1 Answer

Restrict mouse click area 3 Answers

Help rotating gun towards mouse position (with flipping) 1 Answer

Make an object move towards the mouse and then keep going 0 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