• 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 dylancmcd94 · May 13, 2019 at 05:48 AM · raycastpathfinding

Issues with Navmesh Pathfinding and casting a Ray on a transform position

Hello, I'm having issues with my NavMeshAgent pathfinding. So far I have a simple setup in which you can click anywhere on the "Ground" layer in my game and the Player will find a path there. However, I ran into a problem when I found that if you clicked on a "Non-Walkable" baked area, the player wouldn't move at all. I was hoping to solve this by making the player find the closest walkable point near the Obstacle " and then re-order the player to move to that new point. I saw some suggestions about using SamplePositions and FindClosestEdge but I didn't quite understand how they worked so I figured I'd try something a little simpler.

About the code: spawnPoint is a gameObject that I use for positioning things. Basically, I position spawnPoint on the center of the Obstacle that's blocking our Player's movement, then I make the spawnPoint face our Player, and finally I move the spawnPoint forward towards the Player with a loop, moving it Forward by very small increments while detecting if it's hitting any Obstacles via raycast. Once the raycast for Obstacles fails, that means that the spawnPoint isn't hitting an Obstacle so we can then tell our Player to move to the position of the spawnPoint.

This almost works, but I can't seem to get the spawnPoint to move towards the player properly. Also, is there a way to cast a Raycast without having to cast it Forward from the spawnPoint. I just want to detect if the spawnPoint is hitting an Obstacle, whether it be via Tag or Layer it doesn't matter. So how would I Raycast the transform.position of spawnPoint starting from the Camera so it's casting down towards the ground instead of Forward. Also, if anyone knows a better way of going about this that'd be great. Thanks for reading.

 if (Input.GetMouseButtonDown(0))
         {
             Ray castPoint = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
            
             if (Physics.Raycast(castPoint, out hit, Mathf.Infinity, obstacleLayer))
             {
                 var hitPoint = new Vector3(hit.point.x, hit.point.y, hit.point.z);
                 spawnPoint.transform.position = hitPoint;
                 spawnPoint.LookAt(playerAgent.transform.position);

                 for (int i = 1; i < 100; i++)
                 {
                     spawnPoint.localPosition += spawnPoint.forward * 0.20f;
                     RaycastHit hitObs;
                     if (Physics.Raycast(spawnPoint.transform.position, spawnPoint.transform.forward, out hitObs, obstacleLayer))
                     {
                         Debug.Log("Hit an Obstacle");
                     }
                     else
                     {
                         Debug.Log("Found a new Point");
                         point = new Vector3(spawnPoint.position.x, playerAgent.transform.position.y, spawnPoint.position.z);
                         playerAgent.SetDestination(point);
                         isNavMoving = true;
                         playerAgent.transform.LookAt(point);
                         break;
                     }
                 }
             }
         }
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

154 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

Related Questions

AI question, characters escaping from light 0 Answers

Raycast pathfinder nodes 1 Answer

Cube Movement with Raycast 1 Answer

Car AI Avoiding Obstacle 0 Answers

[Solved] Find the position ALONG a raycast 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