• 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 Dwarneke · Oct 02, 2018 at 11:44 PM · programmingnavmeshnavmeshagentmove

Basic Mind Control Mechanic

So I want to create a simple mind control mechanic for a game. The control is predetermined by a script and NavPoints, and I am currently trying to use a basic patrol behavior I found here in Unity documents. What I want to happen, is when the player comes in contact with the NPC, they press a button, and that NPC will travel to the stated navpoint, but I am not having much luck. Here is the script I am tinkering around with:

 using UnityEngine;
 using UnityEngine.AI;
 using System.Collections;
 
 public class WorkerBehavior : MonoBehaviour {
 
     public Transform[] points;
     private int destPoint = 0;
     private NavMeshAgent agent;
 
 
     // Use this for initialization
     void Start () {
 
         agent = GetComponent<NavMeshAgent>();
 
         
         agent.autoBraking = false;
 
     }
 
     void GotoNextPoint()
     {
        
         if (points.Length == 0)
             return;
 
         
         agent.destination = points[destPoint].position;
 
         
         destPoint = (destPoint + 1) % points.Length;
     }
 
     
     void Update () {
         
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (Input.GetKeyDown(KeyCode.KeypadEnter))
         {
             GotoNextPoint();
             
         }
     }
 }

I do understand there is some unnecessary code here. For instance, I have no real need to reset my nav points, as this NPC will only have one destination after it is activated. This patrol behavior may even be useless for what I am trying to do. Any advice would be helpful though, and I thank you in advance.

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
0

Answer by Dwarneke · Oct 03, 2018 at 09:55 PM

Never mind everyone, I figured it out. If anyone is interested however I'll leave my solution here:

 using UnityEngine;
 using UnityEngine.AI;
 using System.Collections;
 
 public class WorkerBehavior : MonoBehaviour {
 
     public Transform[] points;
     private int destPoint = 0;
     private NavMeshAgent agent;
 
 
     
 
     // Use this for initialization
     void Start () {
 
         agent = GetComponent<NavMeshAgent>();
 
       
 
     }
 
     void GotoNextPoint()
     {
         // Returns if no points have been set up
         if (points.Length == 0)
             return;
 
         // Set the agent to go to the currently selected destination.
         agent.destination = points[destPoint].position;
 
         // Choose the next point in the array as the destination,
         // cycling to the start if necessary.
         destPoint = (destPoint + 1) % points.Length;
     }
 
     // Update is called once per frame
     void Update () {
         
     }
 
     void OnTriggerStay(Collider other)
     {
         Debug.Log("Entered");
 
         if (Input.GetKeyDown(KeyCode.Return))
         {
 
             Debug.Log("Return pressed");
             GotoNextPoint();
 
         }
     }

 }

I made some adjustments off of the script I found here: https://docs.unity3d.com/Manual/nav-AgentPatrol.html

This script added to a gameobject, with a collider, will cause the gameobject to move toward a navpoint, when you come in contact with it and press return.

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.

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

105 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

Related Questions

Multiple Cars not working 1 Answer

NavMeshAgent Move experience? 0 Answers

NavMeshAgents rotate to target but do not move 1 Answer

How do I get a navmesh agent to stop at the edge of a collider? 0 Answers

Helicopter demo, unity web player and navmeshagent glichy 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