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 urg0tt · Aug 21, 2012 at 03:15 PM · raycastnavmeshhitworldoutside

How to calculate the very next position on a NavMesh to a cast ray outside the NavMesh?

The situation is: I have a world with a NavMesh attached to it. If I click somewhere in the world the NavMeshAgent moves the character as near as possible to the clicked position. Fine. But the problem is that the Physics.Raycast()-function returns false if I click somewhere outside my "world". Ok that's fine, too. But my question is now how to get the very next position on my NavMesh to the cast ray.

             // Collect all mouse click data.
             MouseClickData data = new MouseClickData();
 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit))
             {
                 data.WorldPosition = hit.point;
                 data.ClickedObject = hit.collider.gameObject;
             }
             data.ScreenPosition = Input.mousePosition;

alt text

alt text

unbenannt.png (129.3 kB)
unbenannt.png (96.2 kB)
Comment
Add comment · Show 4
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 Khada · Aug 21, 2012 at 03:47 PM 0
Share

translate mouse coord into world space, drop its Y value to the level of the terrain and iterate through your meshes verts to find the nearest (or perhaps, grow a collider out from the point and wait till you hit an object).

avatar image Screenhog · Aug 21, 2012 at 04:15 PM 0
Share

Do you want the closest valid position to where you're clicking?

avatar image urg0tt · Aug 22, 2012 at 07:38 AM 0
Share

Yes, of course. I want the closest valid position on my plane or what ever lies next to my click in the "empty world".

To check step by step whether there is an valid point at the left, right, top or bottom is not a solution because there is always a blind angle.

To circle around the point is also a bad solution because in the case of clicking very far away the circle grows a lot and the position where it will find a valid point will be too far away from the position where we would expect the character to go.

$$anonymous$$y next idea would be the to cast a ray from the invalid position (on screen) to the character and then from each of its points a ray down in the scene until a valid position is found. But again there is a problem because we have no guarantee that the character is always in the scene.

I've tried this: http://answers.unity3d.com/questions/294953/search-for-nearest-point-of-object-to-mouseclick.html (last comment) But it seemed to work not properly or not at all. $$anonymous$$ost of the time the character just walks to the center of the plane and not to the borders as I would expect it after clicking in the area behind a border in the empty space. Or maybe I did something wrong. :-/

avatar image Screenhog · Aug 22, 2012 at 04:30 PM 0
Share

When you're clicking outside the world, is there any collider at all? That would be the easiest way to get a position... have an invisible collider.

1 Reply

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

Answer by urg0tt · Aug 23, 2012 at 08:27 AM

I've tried putting a invisible plane under "my world". This approach works more or less well for clicks next to "my world", but clicks far away seem to be thrown away. At least the character (or the NavMeshAgent) doesn't move. An other disadvantage is that the camera always needs to be positioned carefully because otherwise there would be a chance having "empty space" again.

I've also tried to put "my world", including the camera, completely inside a sphere collider, but this doesn't work at all because sending a ray will result in getting an answer immediately as we are already inside a collider.

My current solution is casting a ray from mouse position on screen to the scene. If the ray hits something, everything is fine and the character moves as near as possible. Otherwise I check for every point of the ray the nearby colliders, find the closest collider and save its distance to the point. After all I know the distances of all points to their closest collider. I take the point with the shortest distance, get its closest collider and move the character to a collider's point which lies next to the point of the ray. This approach results in moving my character relatively precisely to a position I would expect. But the algorithm might have performance issues.

So I solved my problem myself. Anyway, thank you for trying to help me. But if you have some further ideas for optimization, just let me know it.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hit distance Change light range 1 Answer

How to hit two object with one raycast? 2 Answers

Need help with Third person shooter 0 Answers

Ray from enemy to player is always a bit off (Image) 1 Answer

RaycastHit.distance 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