• 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
1
Question by IntrovertGenius · Feb 06 at 10:04 AM · 2d gameshooting

Trying to get a player to shoot in Unity 2D.

Hello.

I have a player that I am trying to get to shoot in a Unity 2D game. I would like the bullet to shoot toward the mouse clicking on the screen. From looking around, I came up with this code, but, the problem with it is that the closer to the player that I click, the slower the bullet moves, to almost a stop if I click on the player. What am I doing wrong?

 private float _bulletSpeed = 5.0f;
 
 
 _shootDirection = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - _player.transform.position);
         
         Rigidbody2D bulletInstance = Instantiate(_bullet, _player.transform.position, Quaternion.identity) as Rigidbody2D;
         
         bulletInstance.velocity = new Vector2(_shootDirection.x * _bulletSpeed, _shootDirection.y * _bulletSpeed);


Thank you.

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
1
Best Answer

Answer by privatecontractor · Feb 06 at 12:37 PM

Hi @IntrovertGenius,

Belive that you issue is due to diffrent Vector2 length. So if click point is far from player - Vector2 will have bigger length, if point of click will be close - Vecto2 will have smaller length. Answer for question is Vector2.Normalize - so giving him length of 1 with same direction... then we need to give bullet speed so we just multiplaying this Vector2 by speed, so to conclude, instead of:

bulletInstance.velocity = new Vector2(_shootDirection.x * _bulletSpeed, _shootDirection.y * _bulletSpeed);

write:

bulletInstance.velocity = new Vector2.Normalize(_shootDirection.x, _shootDirection.y) * _bulletSpeed;

Hope that will do, let me know.

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 IntrovertGenius · Feb 07 at 05:53 AM 0
Share

Yes, that worked with a slight adjustment. For some reason it didn't accept it as you typed it.

 bulletInstance.velocity = new Vector2(_shootDirection.x, _shootDirection.y).normalized * _bulletSpeed;


Thank you very much!

avatar image privatecontractor IntrovertGenius · Feb 07 at 06:02 AM 0
Share

Hi @IntroGenius,

For some reason it didn't accept it as you typed it.

Sry my bad, when typing on phone instead of code editor not always able to use code precise enough. But glad could help!

avatar image IntrovertGenius privatecontractor · Feb 07 at 06:07 AM 0
Share

No, it is all good, and perfect. :) I was able to figure it out from what you posted. Thank you so much.

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

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

I need help with a top down 2d shooter 3 Answers

Unity 2D Shoot towards mouse position 1 Answer

Trying to make an enemy fire at the player 1 Answer

How to make my space ship shooting (2D Game)? 0 Answers

OnTrigger bullet with multiple colliders on enemy 1 Answer


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