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 /
avatar image
0
Question by jwalkaz33 · Mar 23, 2020 at 01:31 AM · camera3dmouseposition

Getting the z of mouse position?

I'm setting up a "waypoint" system where I click where I want an object to move but when I use:

     private Camera cam;
     Vector3 point = new Vector3();
 
     Vector3 mousePos = new Vector3();
     public Transform particle;
 
     void Start()
     {
         cam = Camera.main;
     }
 
     void OnGUI()
     {
         Event currentEvent = Event.current;
         // Get the mouse position from Event.
         // Note that the y position from Event is inverted.
         mousePos.x = currentEvent.mousePosition.x;
         mousePos.y = cam.pixelHeight - currentEvent.mousePosition.y;
         
 
         point = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.nearClipPlane));
 
         GUILayout.BeginArea(new Rect(20, 20, 250, 120));
         GUILayout.Label("Screen pixels: " + cam.pixelWidth + ":" + cam.pixelHeight);
         GUILayout.Label("Mouse position: " + mousePos);
         GUILayout.Label("World position: " + point.ToString("F3"));
         GUILayout.EndArea();
     }
 
     private void Update()
     {
         if (Input.GetKeyDown("m"))
         {
             Instantiate(particle, point, Quaternion.identity);
         }
     }

from Unity Docs but the z its uses is the camera position. Is there a way to get the z in world space?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Mar 23, 2020 at 09:00 AM

This is fairly similar to a question I answered earlier today;

https://answers.unity.com/questions/1710147/problem-with-screentoworldpoint-keep-getting-the-s.html?childToView=1710363#answer-1710363

The thing is that expressing the z-coordinate in world space just doesn't really make sense. The x and y coordinates are in screen-space, so when they are converted to world-space chances are they will write to the z-coordinate as well. If you want to find the coordinates in relation to your world, the ScreenPointToRay function is much more applicable - from there you can either do a physics raycast against scene objects, or a plane raycast if your scene has a specific plane along which you wish to find the point;

 //Position of a point that lies on the plane
 public Vector3 planePosition = Vector3.zero;
 //Rotation in degrees of the plane's normal (where its face is pointing)
 public Vector3 planeRotation = new Vector3 (-90f, 0f, 0f);
 
 ...
 
 //z-position doesn't matter
 Ray ray = cam.ScreenPointToRay (mousePos);
 //Create a new plane
 Plane plane = new Plane (Quaternion.Euler (planeRotation) * Vector3.forward, planePosition);
 
 //This will raycast against the physical environment, then fall back to the plane if it misses
 if (Physics.Raycast (ray, out RaycastHit hit))
 {
     point = hit.point;
 }
 else if (plane.Raycast (ray, out float dist))
 {
     point = ray.GetPoint (dist);
 }
Comment
Add comment · Show 6 · 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 jwalkaz33 · Mar 28, 2020 at 09:09 AM 0
Share

After adding your code (If i added it in the way i was meant to), my object just follows the mouse at a strange offset.

     //Position of a point that lies on the plane
     public Vector3 planePosition = Vector3.zero;
     //Rotation in degrees of the plane's normal (where its face is pointing)
     public Vector3 planeRotation = new Vector3(-90f, 0f, 0f);
 
     private Camera cam;
     Vector3 point = new Vector3();
 
     Vector3 mousePos = new Vector3();
     public Transform particle;
 
     //This is the world space z-coord of the points
     float zPosition = 5f;
 
     public Transform unit;
 
     void Start()
     {
         cam = Camera.main;
     }
 
     void OnGUI()
     {
         Event currentEvent = Event.current;
         // Get the mouse position from Event.
         // Note that the y position from Event is inverted.
         mousePos.x = currentEvent.mousePosition.x;
         mousePos.y = cam.pixelHeight - currentEvent.mousePosition.y;
 
         //Z-Position is irrelevant with ScreenPointToRay
         Ray ray = cam.ScreenPointToRay(mousePos);
 
         //Create a new plane on the z-axis
         Plane plane = new Plane(Quaternion.Euler(planeRotation) * Vector3.forward, planePosition);
 
         //Raycast against the plane
         if (plane.Raycast(ray, out float hit))
         {
             //Find the world pos of our screen point on the plane
             Vector3 hitPos = ray.GetPoint(hit);
             unit.position = hitPos;
         }
 
 
         point = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.nearClipPlane));
 
         GUILayout.BeginArea(new Rect(20, 20, 250, 120));
         GUILayout.Label("Screen pixels: " + cam.pixelWidth + ":" + cam.pixelHeight);
         GUILayout.Label("$$anonymous$$ouse position: " + mousePos);
         GUILayout.Label("World position: " + point.ToString("F3"));
         GUILayout.EndArea();
     }
 
     private void Update()
     {
         if (Input.GetKeyDown("m"))
         {
             Instantiate(particle, unit.position, Quaternion.identity);
         }
     }

avatar image Namey5 jwalkaz33 · Mar 28, 2020 at 09:34 AM 0
Share

What exactly is the problem? From what I could understand in the question, that was the desired functionality - to have an object follow the mouse at a specific z-value in world space. If you want the object to be placed on the world-space x/y plane at a specific z-depth, you can change the plane's rotation to [0,0,0].

avatar image jwalkaz33 Namey5 · Mar 29, 2020 at 03:37 AM 0
Share

The problem is that its not on the mouse its above the mouse by a 85 units in the Y rather than at the mouses exact Y position. Red is mouse. Blue is object. alt text

desktop-screenshot-20200328-21391239.png (343.6 kB)
Show more comments

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

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

Related Questions

3D mouse position 0 Answers

Off center projection matrix does not move skybox 0 Answers

I need help with a model 0 Answers

CineMachine without Timeline? 1 Answer

How to force the game to be stretched to fit the screen? 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