• 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 xVarelax · Dec 07, 2013 at 02:03 AM · 2dmouse position

Fire at Mouse Position, 2d game.

Hi guys!

I'm trying to do a aim system where the bullet will be shot in direction of the mouse position...

I've tried to use Input.mousePosition, but after some researchs , I found that t$$anonymous$$s is relative to pixels not to my transform.position.

I've seen too the Camera.main.ScreenToWorldPoint solution, but that does'nt work how I expected.In my game I instantiate the bullet at the transform.position facing the corresponding side, and after that I apply a vector as the velocity of the bullet.

Does t$$anonymous$$s way of firing right or am I'm going in the wrong direction?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Dec 07, 2013 at 07:05 AM

Here is a simple spawner script that, on a mouse click, shoots the 'prefab' at the mouse position. I had gravity scale set to 0 on the prefab.

 #pragma strict
 
 var prefab : GameObject;
 
 function Update () {
     if (Input.GetMouseButtonDown(0)) {
         var pos = Input.mousePosition;
         pos.z = transform.position.z - Camera.main.transform.position.z;
         pos = Camera.main.ScreenToWorldPoint(pos);
         
         var q = Quaternion.FromToRotation(Vector3.up, pos - transform.position);
         var go = Instantiate(prefab, transform.position, q);
         go.rigidbody2D.AddForce(go.transform.up * 500.0);
     }
 }
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 xVarelax · Dec 07, 2013 at 07:35 PM 0
Share
avatar image
1

Answer by coreysnyder · Jul 09, 2014 at 09:55 PM

I'm pretty new to t$$anonymous$$s stuff but I t$$anonymous$$nk you want to use the "Normalize" method seen here. What it does it keep the direction the same but sets the magnitude to 1. Then you can take that direction * Missile speed and that will pretty much guarantee that your missile files the same speed in the correct direction no matter the pre-normalized distance

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 xVarelax · Aug 15, 2014 at 04:31 AM 0
Share

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

18 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

Related Questions

Unity 2D - Bullets don't follow mouse 1 Answer

Limiting a Mouse-follow rotation based on parent object rotation 1 Answer

Mouse Moving in direction of movement 0 Answers

teeworlds eyes animation 2 Answers

Can't make object follow mouse cursor position. 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