Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by dilusion88 · Aug 04, 2012 at 05:49 PM · 2dmousefindcoordinatesfire

Find Mouse co-ordinates on click in a 2d environment

Basically I am attempting to find the x,y coordinates of the mouse cursor when clicking the left mouse button, the reason for this is I have a character with moves along the 2d plane and I want to be able to fire projectiles toward the cursor originating from the character model.

As far as I know the best option is to use ray trace but im not 100% sure how to go about doing so.

Thanks in advance

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

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by darthbator · Aug 04, 2012 at 10:43 PM

You're going to want to make sure that both the actorObject's coordinates and the mouse coords are occupying the same coordinate space. Input.mousePosition returns screen coordinates and your game actors position will be in world coordinates. So I imagine you're going to want to either do a raycast against a plane established from your character with Camera.ScreenPointToRay or you're just going to want to translate the screen coordinates to world coords with Camera.ScreenToWorldPoint. I'm not entirely sure what your game looks like so I can't really say what will give you the coordinates you want as lots of people say they are making a "2d" game but are actually using all 3 dimension of depth inside the engine and just restricting character and object motion to two axes.

Once you're gotten the cursors position in world space coordinates you should be able originate objects from the transform of the players character object and throw them and the location of the cursor.

Comment
Add comment · 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
0

Answer by williampigmeu · Aug 04, 2012 at 05:57 PM

Find X and Y mouse coordinates, calculate the distance between the character that will shot and the mouse, instantiate the projectile, use deltaTime to make it move smoothly to the distance vector.

Comment
Add comment · 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 reptilebeats · Aug 04, 2012 at 06:00 PM 0
Share

only use vector2 as well makes life easier for 2d as you don't use z much.

avatar image dilusion88 · Aug 04, 2012 at 06:37 PM 0
Share

Hi, thanks for the answer, I had an idea it was along those lines however I'm quite new to scripting so am unsure of how to go about it.

I'm not asking for the complete code but if you could point me in the right direction (links to tuts or docs that could help?) that would be awesome.

Thanks again

avatar image williampigmeu · Aug 04, 2012 at 06:47 PM 0
Share

If it helped mark the answer. :3

avatar image williampigmeu · Aug 04, 2012 at 06:48 PM 0
Share

I'll try to make an example.

avatar image
0

Answer by williampigmeu · Aug 04, 2012 at 06:51 PM

For the mouse, have a look at Input.mousePosition. To calculate the distance use a Vector2, something like "distance = Vector2(transform.position, Input.mousePosition);".

Input.mousePosition: http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html

Comment
Add comment · 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 williampigmeu · Aug 04, 2012 at 06:53 PM 0
Share

Also, look at that question.

http://answers.unity3d.com/questions/50953/2d-sidescroller-crosshair.html

avatar image dilusion88 · Aug 04, 2012 at 07:10 PM 0
Share

That's great! Thanks a bunch mate, big help.

avatar image dilusion88 · Aug 04, 2012 at 09:00 PM 0
Share

I think I have managed to get my head round the theory behind the code but I am really struggling with actually putting it into action. Is there any chance you could provide a very simple bit of code I could work off of? thank you again.

avatar image williampigmeu · Aug 04, 2012 at 10:23 PM 0
Share

Oh, I'll try to make an code example...

avatar image
0

Answer by williampigmeu · Aug 04, 2012 at 10:29 PM

Here is it:

var distance; var smooth: float = 0.5

function Update() { if (Input.GetButtonDown("Fire1")) { distance = Vector2(transform.position, Input.mousePosition);

     transform.Translate(distance, smooth);
 }

}

Or something like that, this is only an example, it doesn't really work.

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 williampigmeu · Aug 04, 2012 at 10:36 PM 0
Share

Obs.: In this example, it is supposed to be attached to the bulled, and the bullet to be in "back" (Z coord) of the weapon, like inside the weapon, but, I said, only an example.

avatar image dilusion88 · Aug 05, 2012 at 11:06 AM 0
Share

Thanks again for the help, unfortunately I just cant seem to master it so my project will have to do without for now haha.

avatar image reptilebeats · Aug 05, 2012 at 01:56 PM 0
Share

Do something else, watch some tutorials do some simple stuff, thats what i did and then when i went back to someyhing i did not know, it became easier to grasp.

avatar image
0

Answer by John_South · Jul 08, 2014 at 10:27 AM

if (Input.GetMouseButtonDown(0)) {
var pos = Input.mousePosition; pos = Camera.main.ScreenToWorldPoint(pos);

     var q = Quaternion.FromToRotation(Vector3.up, pos - transform.position);
     Rigidbody2D go = Instantiate(bulletPrefab, transform.position, q) as Rigidbody2D;
     go.rigidbody2D.AddForce(go.transform.up * 500);
 }
Comment
Add comment · 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

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

11 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

Related Questions

how to drag an object using 2d raycasts 0 Answers

Preventing mouseclicks outside the game window 1 Answer

I am making a 2d game and I want to make it where to can pick up bricks with your mouse 3 Answers

c# 2D top down, fire projectale which continuously move in a direction 3 Answers

2D coordinate grid over a 3D scene? 3 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