• 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 imaethan · Jun 13, 2014 at 01:27 PM · movementnavmeshjavanav mesh

Implementing nav mesh into point and click movement

So I've got a basic point and click movement script but whenever I've tried to implement using a navmesh it doesn't really work correctly. I've got the nav mesh set up properly, as I've got some basic AI that uses it and follows the player. Any help would be much appreciated.

      var clickEffect : GameObject;
 
         private var targetPosition:Vector3;
 
         var speed = 60;
 
         
          
 
         function Update () {
         
         
         
        
             if(Input.GetKeyDown(KeyCode.Mouse0))
 
             {
 
            speed = 5;
 
             smooth=1;
 
             
 
                 var playerPlane = new Plane(Vector3.up, transform.position);
 
                 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
                 var hitdist = 0.0;
 
                
 
                 if (playerPlane.Raycast (ray, hitdist)) {
 
                     var targetPoint = ray.GetPoint(hitdist);
 
                     targetPosition = ray.GetPoint(hitdist);
 
                     var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
 
                     transform.rotation = targetRotation;
 
                     
 
                 
 
                 }
 
              
 
             }
 
             
 
                 var dir:Vector3 = targetPosition - transform.position;
 
     var dist:float = dir.magnitude;
 
     var move:float = speed * Time.deltaTime;
 
     if(dist > move){
 
     transform.position += dir.normalized * move;
     Instantiate(clickEffect,targetPosition,Quaternion.identity);
     
     
     
     animation.CrossFade ("run");
 
     } else {
 
     transform.position = targetPosition;
     
     animation.CrossFade ("idle");
 
     }
 
            
 
            
 
             transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime; 
 
            
 
       
 
          
 
          
 
          }
          
          
          function OnCollisionEnter (hit : Collision) {
          
          if (hit.gameObject.tag == "wall") {
          
          print ("hit");
          
                   targetPosition = transform.position;
                   
                   
          
          }
          
          
          
          }
Comment
Add comment · Show 7
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 stuart6854 · Jun 13, 2014 at 01:31 PM 0
Share

Sorry, what doesn't work/happen?

avatar image imaethan · Jun 13, 2014 at 01:53 PM 0
Share

Well this is my basic movement script. I'm trying to add so that it follows the navmesh ins$$anonymous$$d of just trying to run straight through objects. I tried adding in some navmesh commands but it didn't work. For example I would have "var navComponent : Nav$$anonymous$$eshAgent;" at the top.

then under function Start I would have:

 "navComponent = this.transform.GetComponent(Nav$$anonymous$$eshAgent);"

Just not sure if this next bit is correct and where to put it.

 "navComponent.SetDestination(target.position);"


This is my movement (link)

avatar image iwaldrop · Jun 13, 2014 at 05:16 PM 0
Share

What do you mean by, 'doesn't work correctly'? It appears to be working correctly to me. Perhaps you mean it's not working as desired. How would you desire it work?

avatar image imaethan · Jun 13, 2014 at 05:23 PM 0
Share

Like with the navmesh being used. So that when let's say, there's an object in the way. He runs around it, via the navmesh. Ins$$anonymous$$d of straight into it.

alt text

Sorry I didn't make that clear.

avatar image iwaldrop · Jun 13, 2014 at 05:37 PM 0
Share

It seems like you have colliders that allow you to click the walls. If you want to click a wall, but walk around to the other side, then I'd suggest using colliders on a certain layer, and constrain your raycast to that layer only. This way you can control which surfaces in the world respond to touching.

avatar image iwaldrop · Jun 13, 2014 at 06:51 PM 0
Share

Another possibility is that you haven't added Nav$$anonymous$$eshObstacles to some objects that should have them. The docs don't mention whether this is a pro feature or not, but they cut a hole in the navmesh when objects which have them are placed or moved.

Watching your video again, when you click on the wall for instance, what do you expect it to do? Generally speaking, the Nav$$anonymous$$eshAgent is doing what it's supposed to do. Adding special geometry to keep the Nav$$anonymous$$esh from being too close to walls is a possibility, but you can also do a raycast forward while moving and stop if the raycast hits anything. The length of the ray would be the distance at which you'd want the character to stop moving at away from a wall.

avatar image imaethan · Jun 13, 2014 at 07:20 PM 0
Share

It's cut holes where the objects are. I've got a basic AI to use the navmesh, it follows the player and goes around objects to do so. The video was just a demonstration of how the movement works. If you look at the image of the scene I posted, if I clicked the other side of the wall he wouldn't go around, he'd just walk into the wall and stop.

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

23 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

Related Questions

Gaps in the Nav mesh 0 Answers

Navmesh baking -- Odd results 1 Answer

Weird jumpy motion of character in Navigation Fundamentals proect 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How would I simulate a rectangular Nav Mesh Agent? 0 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