Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 zero3growlithe · Oct 31, 2011 at 01:53 PM · mousemouse position

How to get mouse cursor position on plane and set an object's position at this point?

I have a simple code where i have vert/horiz rotation enebled cannon controled using arrows, and what i want to do now is to take control over this cannon with mouse. What i need is to get mouse position on invisible plane and move my 3D cursor to place where mouse cursor collides with plane using raycast... and here's my problem: How do i get this position and use it later on?

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 syclamoth · Oct 31, 2011 at 02:37 PM 0
Share

Have a Vector3 member variable on your script, which you can call on later.

2 Replies

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

Answer by aldonaletto · Oct 31, 2011 at 03:02 PM

You must choose a point and a normal vector to create the plane, use camera.ScreenPointToRay to create a ray from the mouse pointer, Plane.Raycast to find the distance from the ray start and Ray.GetPoint to finally find the 3D point. The script below creates a horizontal plane passing through the object to which the script is attached, create the ray, do the raycast etc.:

function Update(){
    // this creates a horizontal plane passing through this object's center
    var plane = Plane(transform.position, Vector3.up);
    // create a ray from the mousePosition
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    // plane.Raycast returns the distance from the ray start to the hit point
    var distance: float;
    if (plane.Raycast(ray, distance)){
        // some point of the plane was hit - get its coordinates
        var hitPoint = ray.GetPoint(distance);
        // use the hitPoint to aim your cannon
    }
}
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 zero3growlithe · Oct 31, 2011 at 06:43 PM 0
Share

Big Thanks aldonaletto! I can finally move on forward in this JavaScripted madness ^^/

avatar image
3

Answer by The_Mean_Fiddler · May 24, 2015 at 02:38 PM

Thanks for this. It works great

You got a bit wrong though. it's var plane = Plane(Vector3.up, transform.position);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Move Bounderies 0 Answers

Mouse Position to 2D array 1 Answer

Continuously rotating horizontally a player with the mouse position 1 Answer

How can I put range on the mousewheel in three different axis? 0 Answers

How to move game object towards mouse on x and z only? 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