Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DominikHeyn · Sep 15, 2016 at 09:16 AM · mousepositionray

Store and use current mouse position properly

Hey folks,

I'm struggling with storing and using the current mouse position for days now.

I'm working on a "mini MOBA game" and I want to create a skillshot ability where the player selects the ability first and then the direction (or position) of its determination.

I've played around with this line of code:

 ray = Camera.main.ScreenPointToRay (Input.mousePosition);

But it stores the x axis only (in my case). Technically I need the x and the z axis to achieve this. On a further click the created Gameobject should move to the selected position, obviously.

Do you have any tips or code examples for me? Or do you have another advice how to solve this?

Thank you very much in advance!

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 DominikHeyn · Sep 15, 2016 at 07:06 PM 0
Share

Yay I made it. But I'm facing another Problem now :( The projectile is created as supposed but it never reaches its destination. It kind of "drops" in front of the player. I've tried to add force and velocity to the rigidbody but it didn't solve my problem. Do you have any suggestions for me? Thank you very much in advance folks.

2 Replies

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

Answer by DominikHeyn · Sep 17, 2016 at 04:34 PM

Thanks for your answer. I figured it out that this is just a Vector2.

I solved this problem by coding this:

   Plane plane = new Plane(Vector3.up, transform.position);
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

And then continuing with an if statement:

 if (plane.Raycast (ray, out point))
         {
             // do stuff
         }
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 toddisarockstar · Sep 15, 2016 at 07:58 PM

first of all mouse position doesnt have a "Z". its two dimentional as X and Y. its a Vector2.

so to just get the mouse position you would say:

     public float mx;
     public float my;
     
     void Update () {
         mx = Input.mousePosition.x;
         my = Input.mousePosition.y;
     }

keep in mind that mouse results are upside down from GUI coordinates.

if you are looking for what the mouse is on top of in three dimentional space then you need to raycast from the camera into the scene and see what it hits.

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

69 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 avatar image avatar image

Related Questions

How to create a script that will move a object with the mouse(in the x and y axis only) and have my player model head look at it? 1 Answer

The raycast is allways looking to the camera. 0 Answers

How can I spawn an object where I clicked in a 3D space, but spawn it in a 2D location?,How can I spawn objects where I click in a fixed position on 2 axis? 0 Answers

2D Platformer, Rotate sprite/arm towards cursor not working [C] 0 Answers

Another mouse position question... 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