• 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 SgtPancakes · Jun 27, 2014 at 09:10 PM · 2dshootingmouseclick

Shoot towards mouse in Unity2D

I've been having trouble figuring out how to shoot a projectile in constant speed from the location of my character to the mouse position, and let it continue after reaching the mouse position, all while ignoring gravity, as in, a straight line.

I've managed to make the projectile be at the right angle, but couldn't make it travel towards, and through, the mouse.

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
7

Answer by Omberone · Jun 27, 2014 at 10:50 PM

 Vector2 direction = target - myPos;
 direction.Normalize();
 GameObject projectile = (GameObject)Instantiate( projectilePrefab, myPos, Quaternion.identity);
 projectile.rigidbody2D.velocity = direction * speed;

if you want to get the mouse cursor's position in world coordinates all you have to do is

 Vector2 cursorInWorldPos = Camera.main.ScreenToWorldPoint( Input.mousePosition );
Comment
Add comment · Show 7 · 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 SgtPancakes · Jun 28, 2014 at 12:18 AM 0
Share

It doesnt appear to work, the bullets just kind of fall with slight movement towards the clicking position.

This is my code: public class Shooting : $$anonymous$$onoBehaviour {

     public GameObject bullet;
     public float speed = 5.0f;
 
     // Use this for initialization
     void Start () {
     
     }
     
     void Update () {
         if (Input.Get$$anonymous$$ouseButtonDown(0)) {
             Vector2 target = Camera.main.ScreenToWorldPoint( new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y) );
             Vector2 myPos = new Vector2(transform.position.x,transform.position.y + 1);
             Vector2 direction = target - myPos;
             direction.Normalize();
             GameObject projectile = (GameObject)Instantiate( bullet, myPos, Quaternion.identity);
             projectile.rigidbody2D.velocity = direction * speed;
         }
     }
 }

EDIT: never $$anonymous$$d, i just forgot to change the bullet's mass and gravity.

But there is another problem, the bullet doesnt rotate towards its flight direction, how to i fix that?

avatar image Omberone · Jun 28, 2014 at 12:24 PM 1
Share

Quaternion rotation = Quaternion.Euler( 0, 0, $$anonymous$$athf.Atan2 ( direction.y, direction.x ) * $$anonymous$$athf.Rad2Deg );

avatar image SgtPancakes · Jun 28, 2014 at 02:47 PM 1
Share

Well, it sort of worked, it now faces its direction with its body, rather than its tip. Its like the bullet hits the target with its broad side rather than its head, if that sort of makes sense.

this is my implementation:

 if (Input.Get$$anonymous$$ouseButtonDown(0)) {
             Vector2 target = Camera.main.ScreenToWorldPoint( new Vector2(Input.mousePosition.x,  Input.mousePosition.y) );
             Vector2 myPos = new Vector2(transform.position.x,transform.position.y + 1);
             Vector2 direction = target - myPos;
             direction.Normalize();
             Quaternion rotation = Quaternion.Euler( 0, 0, $$anonymous$$athf.Atan2 ( direction.y, direction.x ) * $$anonymous$$athf.Rad2Deg );
             GameObject projectile = (GameObject) Instantiate( bullet, myPos, rotation);
             projectile.rigidbody2D.velocity = direction * speed;
         }

avatar image SudoPhalanx SgtPancakes · Jul 29, 2017 at 04:14 AM 0
Share

Hey I used this code and it works great, but the bullet starts in the middle of my player. How do I get it to start at the tip of the player?

avatar image FunkyPhoenixGames SgtPancakes · Apr 08, 2018 at 07:06 PM 0
Share

Thanks, this code really helped a lot =).

avatar image Omberone · Jun 28, 2014 at 03:58 PM 0
Share

that depends on where your bullet is facing in the prefab =) Is the tip pointing up, right, left or down?

avatar image SgtPancakes · Jun 28, 2014 at 05:22 PM 1
Share

In the prefab it's facing up. Edit: got it! :D

 Quaternion rotation = Quaternion.Euler( 0, 0, $$anonymous$$athf.Atan2 ( direction.y, direction.x ) * $$anonymous$$athf.Rad2Deg + 90 );
avatar image
0

Answer by BenBL · Oct 13, 2017 at 03:06 PM

Anyone can explain Why we had to +1 in position.y? Thanks.

Vector2 myPos = new Vector2(transform.position.x,transform.position.y + 1);

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 FunkyPhoenixGames · Apr 08, 2018 at 07:05 PM 0
Share

It may have had something to do with the size and/or shape of the character SgtPancakes was using; you can safely remove it if it is annoying though =).

@BenBL

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

28 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

Related Questions

List of gameobjects attached to single object 0 Answers

RayCast2D direction is always showing origin point(0,0,0) on game and ignores direction Vector2D.(Unity 2017.4.7.f1) 0 Answers

Top Down Shooting Not Working 1 Answer

Delete certain gameobjects with mouseclicks. 1 Answer

2D character controller getting stuck on walls 5 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