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 /
  • Help Room /
avatar image
0
Question by carle13 · Sep 02, 2016 at 12:51 PM · ainavmeshnavmeshagentcar

How to make a navmesh agent ignore navmesh borders

I'm making a game where you have to run from the police, but i'm having some issues with the AI of the police car. I'm trying to use navmesh agent with physics so I'm using navmeshagent.steering target to make the car steering. In the navmesh I have some gaps because I want the cars to move through the main road but if they cross the gaps I want them to keep moving outside the road but when they cross the gap the navmesh agent stays in the road and doesn't cross it with the car. I've tried turning the navmeshagent.updateposition to false and changing it by myself but it stills happening, so I wanted to know if there's a way to make the navmesh don't stay in the gaps of the navmesh.

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 carle13 · Sep 03, 2016 at 11:00 PM

Well maybe it's not the best way to do it but I made it by enabling and desabling the navmeshagent component. I put it in a coroutine so it wouldn't do it each frame. Here I let you my code so you can see exactly how i did it. It's a Little messy but i hope it helps you.

The important things are on IEnumerator calcularCamino, void GetSteer, void start, and the beginning of the update using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class MovimientoPolicia : MonoBehaviour {
 
     public int TD1_TT2_TA3;
     public float maxTorque;
     public float maxBrake;
     public float maxSpeed;
     public float maxRevSpeed;
     public float maxSteer;
 
     public float velocidadActual;
 
     private Rigidbody rg;
     public Transform centerOfMass;
 
     public WheelCollider[] wheelColliders = new WheelCollider[4];
     public Transform[] tireMeshes = new Transform[4];
 
     private float vueltasRueda;
     public float vs;
 
     private GameObject objeto;
     private Transform seguirObjeto;
 
     public float steering;
     private bool reversa = true;
 
     private bool deteccion = false;
     private float reverCounter = 0f;
     public float waitToReverse = 2f;
     public float reverFor = 1.5f;
 
     private int contadorReversas;
     public MeshRenderer mesh;
 
     public NavMeshAgent nav;
     private NavMeshPath path;
 
     private float spawnX;
     private float spawnZ;
     private Vector3 pos;
 
     private float separacion;
 
     void Start()
     {
         rg = GetComponent<Rigidbody>();
         rg.centerOfMass = centerOfMass.localPosition;
         objeto = GameObject.FindGameObjectWithTag("Carro");
         seguirObjeto = objeto.transform;
         path = new NavMeshPath();
         nav.SetDestination(seguirObjeto.position);
         StartCoroutine("calcularCamino");
         nav.updatePosition = false;
     }
 
     void Update()
     {
         UpdateMeshesPositions();
         GetSteer();
         nav.nextPosition = transform.position;
         switch (TD1_TT2_TA3)
         {
             case 1:
                 vueltasRueda = wheelColliders[0].rpm;
                 break;
             case 2:
                 vueltasRueda = wheelColliders[2].rpm;
                 break;
             case 3:
                 vueltasRueda = wheelColliders[2].rpm;
                 break;
             default:
 
                 break;
         }
         float radioV = ((2 * Mathf.PI * wheelColliders[3].radius) * vueltasRueda) * 60 / 1000;
         vs = Mathf.Round(radioV);
         velocidadActual = Mathf.Abs(vs);
 
         if (rg.velocity.magnitude > 2)
         {
             contadorReversas = 0;
         }
 
         if (rg.velocity.magnitude < 0.1 && !reversa)
         {
             reverCounter += Time.deltaTime;
             if (reverCounter >= waitToReverse)
             {
                 reverCounter = 0;
                 reversa = true;
                 contadorReversas ++;
 
                 if (contadorReversas > 4 && !mesh.isVisible && rg.velocity.magnitude < 0.1)
                 {
                     transform.rotation = Quaternion.Euler(0, transform.rotation.y, 0);
                     spawnX = seguirObjeto.position.x + Random.Range(10, 20);
                     spawnZ = seguirObjeto.position.z + Random.Range(10, 20);
                     pos = new Vector3(spawnX, 0, spawnZ);
                     transform.position = pos;
                 }
             }
         } else if (!reversa)
         {
             reverCounter = 0;
             steering *= 1;
         }
 
         if (reversa)
         {
             steering *= -1;
             reverCounter += Time.deltaTime;
             if (reverCounter >= reverFor)
             {
                 reverCounter = 0;
                 reversa = false;
             }
         }
 
         Vector3 dif = transform.position - seguirObjeto.position;
         separacion = dif.sqrMagnitude;
         if (separacion > 40000)
         {
             transform.rotation = Quaternion.Euler(0, transform.rotation.y, 0);
             spawnX = seguirObjeto.position.x + Random.Range(10, 20);
             spawnZ = seguirObjeto.position.z + Random.Range(10, 20);
             pos = new Vector3(spawnX, 0, spawnZ);
             transform.position = pos;
         }
     }
 
     void FixedUpdate()
     {
         wheelColliders[0].steerAngle = -steering;
         wheelColliders[1].steerAngle = -steering;
 
         if (!reversa)
         {
             switch (TD1_TT2_TA3)
             {
                 case 1:
                     if (velocidadActual < maxSpeed)
                     {
                         //acelerar delantera
                         wheelColliders[0].motorTorque = maxTorque;
                         wheelColliders[1].motorTorque = maxTorque;
                     }
                     else
                     {
                         wheelColliders[0].motorTorque = maxTorque / 32;
                         wheelColliders[1].motorTorque = maxTorque / 32;
                     }
                     break;
                 case 2:
                     if (velocidadActual < maxSpeed)
                     {
                         //acelerar trasera
                         wheelColliders[2].motorTorque = maxTorque;
                         wheelColliders[3].motorTorque = maxTorque;
                     }
                     else
                     {
                         wheelColliders[2].motorTorque = maxTorque / 32;
                         wheelColliders[3].motorTorque = maxTorque / 32;
                     }
                     break;
                 case 3:
                     if (velocidadActual < maxSpeed)
                     {
                         //acelerar cuatro
                         wheelColliders[0].motorTorque = maxTorque;
                         wheelColliders[1].motorTorque = maxTorque;
                         wheelColliders[2].motorTorque = maxTorque;
                         wheelColliders[3].motorTorque = maxTorque;
                     }
                     else
                     {
                         wheelColliders[0].motorTorque = maxTorque / 32;
                         wheelColliders[1].motorTorque = maxTorque / 32;
                         wheelColliders[2].motorTorque = maxTorque / 32;
                         wheelColliders[3].motorTorque = maxTorque / 32;
                     }
                     break;
                 default:
 
                     break;
             }
         }
         else if (reversa)
         {
             switch (TD1_TT2_TA3)
             {
                 case 1:
                     if (velocidadActual < maxRevSpeed)
                     {
                         //acelerar delantera
                         wheelColliders[0].motorTorque = -maxTorque;
                         wheelColliders[1].motorTorque = -maxTorque;
                     }
                     else
                     {
                         wheelColliders[0].motorTorque = -maxTorque / 32;
                         wheelColliders[1].motorTorque = -maxTorque / 32;
                     }
                     break;
                 case 2:
                     if (velocidadActual < maxRevSpeed)
                     {
                         //acelerar trasera
                         wheelColliders[2].motorTorque = -maxTorque;
                         wheelColliders[3].motorTorque = -maxTorque;
                     }
                     else
                     {
                         wheelColliders[2].motorTorque = -maxTorque / 32;
                         wheelColliders[3].motorTorque = -maxTorque / 32;
                     }
                     break;
                 case 3:
                     if (velocidadActual < maxRevSpeed)
                     {
                         //acelerar cuatro
                         wheelColliders[0].motorTorque = -maxTorque;
                         wheelColliders[1].motorTorque = -maxTorque;
                         wheelColliders[2].motorTorque = -maxTorque;
                         wheelColliders[3].motorTorque = -maxTorque;
                     }
                     else
                     {
                         wheelColliders[0].motorTorque = -maxTorque / 32;
                         wheelColliders[1].motorTorque = -maxTorque / 32;
                         wheelColliders[2].motorTorque = -maxTorque / 32;
                         wheelColliders[3].motorTorque = -maxTorque / 32;
                     }
                     break;
                 default:
 
                     break;
             }
         }
     }
 
     void GetSteer()
     {
         Vector3 steerVector = transform.InverseTransformPoint(nav.steeringTarget);
         steering = maxSteer * (steerVector.x / steerVector.magnitude);
     }
 
     IEnumerator calcularCamino ()
     {
         nav.enabled = false;
         nav.enabled = true;
 
         NavMesh.CalculatePath(transform.position, seguirObjeto.position, NavMesh.AllAreas, path);
         nav.path = path;
 
         yield return new WaitForSeconds(0.5f);
 
         StartCoroutine("calcularCamino");
     }
 
     void UpdateMeshesPositions()
     {
         for (int i = 0; i < 4; i++)
         {
             Quaternion quat;
             Vector3 pos;
             wheelColliders[i].GetWorldPose(out pos, out quat);
 
             tireMeshes[i].position = pos;
             tireMeshes[i].rotation = quat;
         }
     }
 }
 
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.

Follow this Question

Answers Answers and Comments

98 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

Related Questions

how to search for a random point until the condition is true? 0 Answers

Why my second NavMesh Agent can't find the NavMesh? 1 Answer

Second NavMesh Surface refuses to cooperate: Failed to create agent because it is not close enough to the NavMesh 0 Answers

Farthest Reachable Destination 0 Answers

How to set closer point to destination on NavMeshAgent? 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