Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Enigmaticall · Jun 24, 2014 at 06:49 AM · bullettopdownshooter

2D Shoot at Mouse Position; how to rotate towards the mouse?

I am trying to create the shooting mechanic for a 2D topdown shooter. I need to shoot bullets at the location of the mouse. I have the code working which instantiates the bullets and shoots them in different directions based on the mouse, but the problem is that Input.mousePosition uses pixel coordinates that are not based on the position of the camera or player. By this, I mean that the bullets only shoot as they should when my character stands on (0, 0) of the pixel coordinates.

 var mousePos: Vector2 = Input.mousePosition;
 var bulletClone : Rigidbody2D = Instantiate(bullet, transform.position, transform.rotation);
 bulletClone.rigidbody2D.AddForce(mousePos);

Also, I do not know how to rotate the bullets to the face the mouse. If I was able to do that, it would be very easy to move the bullets towards the mouse. If anyone can help me with this predicament, I would be very very appreciative. Thank you kindly!

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 robertbu · Jun 24, 2014 at 06:55 AM

Here is a solution. I'm assuming the camera is Orthographic. It uses Camera.ScreenToWorldPoint() for the conversion, and it use Mathf.Atan2() to generate the rotation. Note this code assumes that your object is a sprite and that the 'forward' of the bullet is facing right when the rotation is (0,0,0):

 var mousePos: Vector2 = Input.mousePosition;
 mousePos = Camera.main.ScreenToWorldPoint(mousePos);
 var dir = mousePos - transform.position;
 var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 var bulletClone : Rigidbody2D = Instantiate(bullet, transform.position, transform.rotation);
 bulletClone.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 bulletClone.AddForce(bulletClone.forward * amount);
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 Enigmaticall · Jun 24, 2014 at 05:42 PM 0
Share

I am not sure if my long response registered, as it is not appearing here, but this worked perfectly and I CANNOT thank you enough(I got my other problem resolved as well that I had put in the orginal response). I spent 5 and half hours trying to figure this out, and I was able to simply place your code into my project. I know that you are very active on this site, so thank you again for helping everyone out!

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

2 People are following this question.

avatar image avatar image

Related Questions

Top down shooter, bullet movement issues 3 Answers

2D Tanks Shooting in right direction 0 Answers

add force on topdown bullet 1 Answer

topdown shooter unity 2 Answers

on trigger in top down shooter 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