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 One-Zero · Oct 03, 2015 at 08:14 PM · navmeshnavmeshagent

NavMashAgent not moving in Unity3D 5

Here is my code. Im trying to move a Cube to CheckPoints. FindClosestTarget() method collect all Checkpoints. When Cube hit the CheckPoint, ChackPoint will be Set as Inactive. When the Collision happens FindClosestTarget() call again and get the new closest CheckPoint. Thing is Cube not moving. It Look at the closest check point but not moving. I'm I doing something wrong here?

 using UnityEngine;
 using System.Collections;
 
 public class EnimyAI03N : MonoBehaviour {
 
     NavMeshAgent agent;
     Transform Target;
     private float rotationSpeed=15.0f;
 
     void Start () {
         agent = GetComponent<NavMeshAgent>();
         Target = FindClosestTarget ().transform;
     }
 
     void Update () 
     {
         LookAt (Target);
         MoveTo (Target);
 
     }
 
     // Turn to face the player.
     void  LookAt (Transform T){
         // Rotate to look at player.
         Quaternion rotation= Quaternion.LookRotation(T.position - transform.position);
 
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime*rotationSpeed);
         //transform.LookAt(Target); alternate way to track player replaces both lines above.
 
     }
 
     void  MoveTo (Transform T){
         agent.SetDestination(T.position); 
     }
 
     void OnCollisionEnter (Collision col)
     {
 
         if(col.gameObject.name.Contains("CheckPoint"))
         {
             Target = FindClosestTarget ().transform;
         }
     }
 
     GameObject FindClosestTarget() {
         GameObject[] gos;
         gos = GameObject.FindGameObjectsWithTag("CheckPoint");
 
         GameObject closest=null;
         float distance = Mathf.Infinity;
         Vector3 position = transform.position;
         foreach (GameObject go in gos) {
             Vector3 diff = go.transform.position - position;
             float curDistance = diff.sqrMagnitude;
 
             if (curDistance < distance) {
                 closest = go;
                 distance = curDistance;
             }
         }
         return closest;
     }
 
 }
Comment
Add comment · Show 1
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 Hullu · Oct 05, 2015 at 08:51 AM 0
Share

Are you sure the path is valid?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ziron999 · Oct 04, 2015 at 10:14 AM

Use destination instead of SetDestination in your MoveTo function. Also use Physics sphereoverlap with a filterlayer it will be much more effective then using triggers and easier to control what's going on.

Comment
Add comment · Show 4 · 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 One-Zero · Oct 04, 2015 at 05:47 PM 0
Share

destination also not working.

avatar image Ziron999 · Oct 04, 2015 at 06:42 PM 0
Share

Use overlap sphere to get your target. It has a radius param that can be changed on the fly easily. If target = null expand radius. Then if target radius = 0(or whatever you think the $$anonymous$$imum needs to be) Use a filterlayer to only check for these points.

avatar image One-Zero · Oct 05, 2015 at 02:52 AM 0
Share

Thanks for the responce Ziron999. Can you show me how to do it. I still can not make this working. Because Im new to Unity development

avatar image Ziron999 · Oct 05, 2015 at 11:38 PM 0
Share

There is luckly already hundreds of examples for this here is a link to one.http://answers.unity3d.com/questions/45534/how-is-overlapsphere-used.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Navmesh Teleporting (With Server) 1 Answer

How to sample a point on a navMesh Agent path 0 Answers

NavMeshAgent not calculating shortest route. 3 Answers

Make Navmesh Agents more consistent 0 Answers

Enemy follow Camera 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