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 MIHGames · Dec 18, 2014 at 01:35 PM · navmeshnewbiepoint

Simple NPC AI using Nav Mesh

Hi Everybody, Here is my problem. I've made a C# script to navigate a NPC(no animation yet, real simple) and if you look at my script you (should) see the problem.

 public class NPCTestNav : MonoBehaviour {
 
     NavMeshAgent agent;
     public Transform Nav1;
     public Transform Nav2;
     public Transform Nav3;
     public Transform Nav4;
     int RandomValue;
 
 
     // Use this for initialization
     void Start () 
     {
         agent = GetComponent<NavMeshAgent> ();
     }
     
     // Update is called once per frame
     void Update () 
     {
         RandomValue = Random.Range (1, 5);
         Debug.Log (RandomValue);
         if (RandomValue == 1)
         {
             //This is part of a code snippit and will wreck the code if uncommented.
                 //Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                 
             Ray Position1 = new Ray (Nav1.position, Input.mousePosition);
             RaycastHit hit1;
             if (Physics.Raycast (Position1, out hit1, 1000)) 
             {
                 agent.SetDestination (hit1.point);
             }
         }
         if (RandomValue == 2)
         {
             Ray Position2 = new Ray (Nav2.position, Input.mousePosition);
             RaycastHit hit2;
             if (Physics.Raycast (Position2, out hit2, 1000)) 
             {
                 agent.SetDestination (hit2.point);
             }
         }
     }
 }
 

As you can see, it assigns a new Destination every few frames. I currently have only 4 destination points, but have not got that far yet, just trying to work out two points. So, my solution just add a simple script to get the speed of the NPC and then add an && to the if asking if the avatar is moving. However, whenever I try to add the code, it always comes up as not moving???? What is up here?

EDIT: Ok so to clarify, I want an NPC that will, every frame choose randomly with a 1in4 chance a new Destination unless it is already travelling to a destination. There is four destination points, these destination points are cubes.

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
Best Answer

Answer by SkaredCreations · Dec 18, 2014 at 01:43 PM

I think this should be fine (drag on Navs all the transforms where you want the agent can walk to):

 using UnityEngine;
 using System.Collections;
 
 public class NPCTestNav : MonoBehaviour {
 
     public Transform[] Navs;
     NavMeshAgent agent;
 
     void Start () 
     {
         agent = GetComponent<NavMeshAgent>();
     }
     
     void Update () 
     {
         if (agent.velocity.magnitude == 0f)
         {
             agent.SetDestination(Navs[Random.Range(0, Navs.Length)].position);
         }
     }
 }
 
Comment
Add comment · Show 2 · 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 MIHGames · Dec 18, 2014 at 04:12 PM 0
Share

I have updated my post to help clear up what I was trying to do.

avatar image MIHGames · Dec 19, 2014 at 04:46 AM 0
Share

Thanks @SkaredCreations, however after adding a walk animation I had to tweak the statement to <= rather than ==.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to detect if NavAgent has reached hit.point? (2) Distance doesn't work? 1 Answer

Why does Unity crash with this code ? 2 Answers

sprite editor cut a section of a sprite, is possible? 0 Answers

new input system catches inputs too early 1 Answer

What is wrong with this code? (Solved) 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