• 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
Question by Temseii · Sep 02, 2016 at 11:00 PM · instantiatequaterniontransform.positionvector2

Not able to fire projectiles at mouse position

Hey,

I'm trying to add a simple little system that shoots projectiles to the mouses location on click. I've it added and it's shooting projectiles like I want it to, just not at the right direction. It doesn't matter where my mouse is, if my character is on the right side of the scene, the projectiles will shoot left and if it's on the left side, you guessed it - projectiles will fire away to the right.

Any idea what could be causing t$$anonymous$$s? Here's the relevant function.

  void Shoot () {
          if (Input.GetMouseButtonDown(0)) {
          Vector2 mousePos = new Vector2 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
          Vector2 myPos = new Vector2(transform.position.x,transform.position.y);
          Vector2 direction = mousePos - myPos;
          direction.Normalize();
          Quaternion rotation = Quaternion.Euler( 0, 0, Mathf.Atan2 ( direction.y, direction.x ) * Mathf.Rad2Deg);
          GameObject projectile = (GameObject)Instantiate( projectilePrefab, firePoint.position, rotation);
          projectile.GetComponent<Rigidbody2D>().velocity = direction * speed;
         }
     }

Comment
bowserscastle

People who like this

1 Show 0
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

Answer by cezikmertcan · Sep 02, 2016 at 11:45 PM

Try t$$anonymous$$s

   void Shoot ()
   {
     if (Input.GetMouseButtonDown(0))
     {
           Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
           Vector2 myPos = (Vector2)transform.position;
           Vector2 direction = mousePos - myPos;
           direction.Normalize();
           float angle =  Mathf.Atan2 ( direction.y, direction.x ) * Mathf.Rad2Deg;
           GameObject projectile = (GameObject)Instantiate( projectilePrefab);
           projectile.transform.position = firePoint.position;
           projectile.transform.Rotate (0, angle, 0);
           projectile.GetComponent<Rigidbody2D>().velocity = direction * speed;
     }
 }
Comment
bowserscastle

People who like this

1 Show 2 · 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 Temseii · Sep 03, 2016 at 12:32 AM 0
Share

Thanks for the reply! The issue didn't change though.. Tried so many things, it's starting to feel like it's something in the settings and not in the script

avatar image cezikmertcan Temseii · Sep 03, 2016 at 12:18 PM 0
Share

I will try my code and do a better version to post here. Sorry did not have a chance to try this one.

avatar image

Answer by josehzz112 · Sep 03, 2016 at 04:50 PM

I t$$anonymous$$nk I have a better solution

Declare public float angleOffsetInDegrees, change it in the inspector to get the desired rotation in degrees.

 void Shoot() {
     if (Input.GetMouseButtonDown(0)) {
         Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Mouse position to world position
         Vector2 myPos = new Vector2(transform.position.x, transform.position.y);
   
         //Calculate direction in quaternion
         Quaternion targetQuaternion = Quaternion.Euler(Quaternion.LookRotation(Vector3.forward, mousePos - myPos).eulerAngles + new Vector3(0f, 0f, angleOffsetInDegrees));
   
         //Calculate direction in Vec3
         Vector3 direction = new Vector3(Mathf.Sin(-targetQuaternion.eulerAngles.z * Mathf.Deg2Rad), Mathf.Cos(targetQuaternion.eulerAngles.z * Mathf.Deg2Rad), 0f);
         direction.Normalize();
   
         //Instantiate
         GameObject projectile = (GameObject)Instantiate(projectilePrefab, firePoint.position, targetQuaternion);
         projectile.GetComponent<Rigidbody2D>().velocity = direction * speed;
     }
 }

First I calculate the Quaternion setting the foward vector as the pivot for the rotation, and then the Vector3 using the Sin and Cos functions to get the corresponding angle.

Comment

People who like this

0 Show 4 · 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 Temseii · Sep 03, 2016 at 08:48 PM 0
Share

Appreciate the post! That changed thing up a little bit, the projectile is now going directly north no matter where I keep my mouse

avatar image josehzz112 · Sep 04, 2016 at 03:58 AM 0
Share

I fixed the code in my answer.

Hope it works now.

avatar image Temseii josehzz112 · Sep 04, 2016 at 04:10 AM 0
Share

That took it back to the original issue, where if I'm standing on the left side of the screen the projectiles will fire right and vise versa. :/

avatar image josehzz112 Temseii · Sep 04, 2016 at 04:15 PM 0
Share

Updated the code. Now you can add an offset angle value to the calculated one. Change the value of that variable in the inspector from 0 to 360 and test it to get the desired rotation angle.

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

67 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

Related Questions

GameObject moving left to right AND up 1 Answer

Change animation at runtime with Mecanim 0 Answers

How to instantiate prefabs with offset transform.position values? 2 Answers

How to get position of instantiated object before it is destroyed 2 Answers

Instantiated objects always facing the center 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