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
1
Question by gewl · Oct 26, 2017 at 08:54 PM · inputpositionplayermousepositionplane

Best way of accurately getting mouse position in world space on same plane as player?

Hi!

I'm working on a prototype of an action RPG. It's sort of 3D-lite with a locked isometric camera—meaning that the player can go up and down stairs to different vertical levels, fall between areas, etc., but entities' actions are basically limited to the current horizontal plane they're on. So an entity with a gun can't shoot upwards or downwards, they can only shoot along the plane they're currently on.

I'd like the player character to rotate to face the current mouse position, but I'm having a little trouble. Specifically, I've been trying to cast the mouse position into world space as if the mouse is on the same horizontal plane as the player. The way I've been trying this so far has been to have the following in a script on the player entity:

 Vector2 relativeMousePosition = Input.mousePosition - mainCamera.WorldToScreenPoint(transform.position);
 if (relativeMousePosition != Vector2.zero)
 {
     Vector3 newPosition = Quaternion.Euler(0f, cameraRotation, 0f) * new Vector3(mousePosition.x, transform.position.y, mousePosition.y);
     transform.LookAt(newPosition);
 }

This sort of works—according to Debug.DrawLine, the player looks in the right place when the mouse position is directly to the right, to the left, above, or below the player on the screen. At any other angle, though, there's a slight gap between the DrawLine and the mouse position.

I've also poked around with using a Plane set to the player's position, raycasting off the mouse position, and taking the point where the plane and the ray intersect. This hasn't worked so well, and it also seems like it'd be unnecessarily expensive, as it requires translating the plane around with the player.

What's the preferred way of handling this?

Thank you!

Edit: Right after posting this I found a way to get an accurate position from Plane using the following:

  playerPlane = new Plane(Vector3.up, transform.position);
  Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
  float distance;
  Vector3 relativeMousePosition;
  if (playerPlane.Raycast(ray, out distance))
  {
      relativeMousePosition = ray.GetPoint(distance);
  }

This works, but making a new Plane every Update seems expensive—is it?

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
2
Best Answer

Answer by Bunny83 · Oct 26, 2017 at 09:12 PM

The easiest way is to use Unity's Plane struct. It simply defines an infinite mathematical plane which is defined by a single point that is on the plane and a normal vector. Since you want the plane to be parallel to the ground you can simply use the player position as point and "Vector3.up" as normal.

Now you can simply use Camera.ScreenPointToRay to get a ray from the camera into your scene based on the mouse position. Finally just use the planes Raycast method to get the distance of the intersection point between ray and plane. This distance can be used in the GetPoint method of the ray to get the final worldspace position of the intersection.


 public Vector3 GetPlayerPlaneMousePos(Vector3 aPlayerPos)
 {
     Plane plane = new Plane(Vector3.up, aPlayerPos);
     Ray = ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     float dist;
     if (plane.Raycast(ray, out dist))
     {
         return ray.GetPoint(dist);
     }
     return Vector3.zero;
 }


Note if the ray doesn't intersect the plane at all we can't return a position so we return (0,0,0). However this can only happen if the ray is parallel to the plane ot points upwards away from the ground which is probably never the case.

Comment
Add comment · Show 2 · 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 gewl · Oct 26, 2017 at 09:44 PM 0
Share

That's helpful, thank you. Do you have any sense of how expensive this approach would be? It seems like it'd be most straightforward to make a new Plane every time I need this position, as otherwise I have to deal with caching positions to translate it accurately. But that'd mean making a new Plane every frame, because I'm getting the mouse position every frame—seems like a heavy process!

avatar image Bunny83 gewl · Oct 27, 2017 at 12:27 AM 0
Share

This is the cheapest thing ever. All types used here are value types so you don't "create" a new plane. It's a value type just like a Vector3. Also the raycast and GetPoint is very very simple linear algebra.

This code does not allocate a single byte of memory which would need to be garbage collected. The Plane struct just consists of 4 float values (3 for the normal vector and one for the distance from origin.) It just represents a plane equation in normal form

The Raycast method literally is implemented like this:

 // copied from UnityEngine.dll
 public bool Raycast(Ray ray, out float enter)
 {
     float num = Vector3.Dot(ray.direction, this.normal);
     float num2 = -Vector3.Dot(ray.origin, this.normal) - this.distance;
     bool result;
     if ($$anonymous$$athf.Approximately(num, 0f))
     {
         enter = 0f;
         result = false;
     }
     else
     {
         enter = num2 / num;
         result = (enter > 0f);
     }
     return result;
 }

You don't have to understand this code in order to use it. However if you know vector arithmetic it should be quite easy to understand. Likewise Ray.GetPoint is just this:

 public Vector3 GetPoint(float distance)
 {
     return this.m_Origin + this.m_Direction * distance;
 }

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

101 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

Related Questions

Camera rotation around player while following. 6 Answers

How I can save mouse position ? 0 Answers

How to keep position on input.get axis even when it is pressed twice? 1 Answer

Can I fake the mouse/touch position relative to the real mouse/touch position? 0 Answers

Following the cursor... 2 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