• 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
3
Question by Onithec4t · Aug 03, 2014 at 11:58 PM · 2dprojectile

[2D] [C#] Shoot projectile in mouse direction from player position

Hi guys, I'm trying to make a simple 2D shooter, and I need to shoot a projectile from player position to the mouse pointer position. I did this, but it works only if the player is in the center of the world and the projectile speed changes depending of the distance between the click and the player position while I need always the same speed.

 using UnityEngine;
 using System.Collections;
 
 public class ProjController : MonoBehaviour {
     
     public KeyCode shoot;
 
     // Use this for initialization
     void Update () {
     
         if (Input.GetKey (shoot)) {
 
 
             Vector2 mp = new Vector2 (Input.mousePosition.x,Input.mousePosition.y);
             Vector3 wp = Camera.main.ScreenToWorldPoint(new Vector3(mp.x, mp.y, 0f));
 
             rigidbody2D.AddForce (new Vector2(wp.x, wp.y), 0);
 
                 }
     }
 
     void OnTriggerEnter2D(Collider2D other) {
         if (other.gameObject.name == "enemy") {
                         Destroy (other.gameObject);
                 }
         if (other.gameObject.name == "player") {
                         Destroy (this.gameObject);
         
Comment
Add comment · Show 1
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 meat5000 ♦ · Aug 03, 2014 at 11:45 PM 1
Share

http://docs.unity3d.com/$$anonymous$$anual/UnderstandingVectorArithmetic.html

1 Reply

· Add your reply
  • Sort: 
avatar image
11

Answer by robertbu · Aug 04, 2014 at 12:05 AM

Try this:

 void Update () {
 
     if (Input.GetKey (shoot)) {
         Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 dir = (Input.mousePosition - sp).normalized;
         rigidbody2D.AddForce (dir * amount);
 
      }
 }

...where 'amount' is either replaced by a constant or is a variable you define and initialize. I'd start with a larger number...say 500 to start. I'm not sure about the 2D physics calculations, but for 3D with a mass of 1.0, 500 is about the minimum of a one-time shot to get a projectile moving reasonably.

And @meat5000's reference is a good thing to read. You don't need to split up and recombine vectors.

Comment
Add comment · Show 6 · 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 Onithec4t · Aug 04, 2014 at 12:20 AM 0
Share

Thanks man, this solution works great!

avatar image TobiasW Onithec4t · Mar 30, 2016 at 03:21 PM 0
Share

Remember to tag it as "best answer" if it helped you!

avatar image Adrian-Salarda · Mar 30, 2016 at 02:54 PM 1
Share

where does the amount come from?

avatar image KUFgoddess Adrian-Salarda · Dec 20, 2016 at 06:12 AM 1
Share

I think amount is just a place holder so perhaps replace it with a number?

avatar image HarryPease · Jun 26, 2018 at 03:51 PM 0
Share

I'm just curious as to what the SP stands for?

avatar image derekmklinger HarryPease · Jul 22, 2018 at 05:51 PM 0
Share

sp is short for ScreenPoint in this instance.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Raycasting not working as expected: rays are perpendicular to projectile's trajectory and collision doesn't occur 1 Answer

Calculate initial velocity for projectile to reach point at a given angle and calculate angle to reach point at a given velocity. 1 Answer

How to calculate projectile range 2 Answers

(2D) Face Object in Direction of Travel 1 Answer

2D projectile trajectory prediction 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