I have set up a NavMeshAgent on my enemy character that is following a set path. Currently though he turns as he moves which in some cases looks more natural but if the enemy is just walking back and forth it just looks like he is walking backward for a large part of the path which looks less natural. Is there a way to control the rotation within the agent or to set the agent to only move when its angle is within x degrees of forward?
↧
Get NavMeshAgent to look where it's going
↧
Why does my navmesh pathfinding get screwed up after i hit a wall?
i have a creature with a navmesh agent, a rigidbody, and a collider. Here are the parameters on all three
![alt text][1]
Here's the area i'm working with, it's a simple square playpen, with the sides nd floor set to navmesh static. Note the navmesh in the middle. I do not know why this isn't sitting perfectly in there, but it isn't. the navmesh seems to slope upwards towards the lowerleft corner, despite the floor being utterly flat.
![alt text][2]
The puppy can path around this area just fine. i've got it set to run to the spot where ik click, this has no adverse effects. It stops pretty close to the target point:
![alt text][3]
However i have a problem. If i click on the walls of the pen (which are not pathable, as seen in the navmesh picture) then things start to get screwed up. The puppy bounces off them, and then becomes unable to properly navigate to anything until i exit and restart play mode
Here's an example of the puppy bouncing off then getting screwed up:
![alt text][4]
And then after that, until i restart play mode, he becomes unable to come to a rest on a target spot, instead fidgeting around it endlessly, like this:
![alt text][5]
So yea, what gieves?
If the nav mesh agent is given a target it can't path to, shouldn't it either get as close as it can and then stop, or just refuse to move at all? How is the puppy pathing to these areas, and why is it subsequently getting messed up
[1]: http://i.gyazo.com/0e79fc4e573329bd0d41f2fbc8b081fe.png
[2]: http://i.gyazo.com/ec4145ebf1d63e7ad0b9d635c749a0b8.png
[3]: http://i.gyazo.com/fc327a8d16698de1ff9d3a68408c859d.gif
[4]: http://i.gyazo.com/2530603b1764376f3a9711420baed1e6.gif
[5]: http://i.gyazo.com/71d166caff66bddb63568681846e206d.gif
↧
↧
NavMeshAgent Finding New Enemy ON Range/Distance
Hello,
actually when my unit spawns, it goes straight to the Enemy's base to destroy it, but on it's way to there: it has to kill Enemies first! instead of killing the enemies, it goes straight to the base, because the enemies are spawning after the Enemy's base
So the Enemy's base is the first object on the NavMesh... I am stuck...
I want the unit's to "see" the Enemy if it's spawned, when the unit is Walking in a Radius of the Enemy
my Code actually:
using UnityEngine;
using System.Collections;
public class PlayerFindEnemy : MonoBehaviour {
Transform enemy;
NavMeshAgent nav;
void Awake ()
{
enemy= GameObject.FindGameObjectWithTag ("Enemy").transform;
nav = GetComponent ();
}
void FixedUpdate() {
nav.SetDestination (enemy.position);
}
}
This is my code,
can I do anything like a "Refresher to activate the NavMesh - if in Range/Distance again" ?
Thank you for reading
↧
AI pathfinding waypoints
Ive implemented a simple script for random movement between waypoints with a random wait time between each, the problem is the AI seems a little "indecisive", at the start of movement the ai doesnt decide right away which waypoint to go to. It's starting to walk in multiple directions before it decides "thats the waypoint im going to".
anyone see a fix for this?
this is the script:
private bool isDeciding;
void Start () {
agent = GetComponent ();
}
// Update is called once per frame
void Update () {
if (GetComponent ().isTalking ) {
agent.Stop();
agent.transform.LookAt(GameObject.FindGameObjectWithTag("Player").transform.position);
}
if (!GetComponent ().isTalking) {
agent.Resume();
}
if (isWaiting) {
if(!isDeciding)
StartCoroutine(WaitOrMove());
}
}
if (agent.remainingDistance <= agent.stoppingDistance) {
//StartCoroutine(WaitOrMove());
isMoving = false;
isWaiting = true;
}
//agent.SetDestination (GameObject.FindGameObjectWithTag ("Player").transform.position);
}
private IEnumerator WaitOrMove () {
isDeciding = true;
yield return new WaitForSeconds (Random.Range (1, 3));
int r = Random.Range (0, 6);
agent.SetDestination(waypoints[r].transform.position);
isMoving = true;
isWaiting = false;
isDeciding = false;
}
**EDIT:** edited the script with answer
↧
NavMeshAgent.isOnNavMesh
Hey,
I tried to check if my NavmeshAgent is on the Mesh before setting a destination and get this error:
Type `UnityEngine.NavMeshAgent' does not contain a definition for `isOnNavMesh' and no extension method `isOnNavMesh' of type `UnityEngine.NavMeshAgent' could be found (are you missing a using directive or an assembly reference?)
I searched for this issue but couldn't find any solutions for it.
↧
↧
Survival Shooter Tutorial fix for Unity 5
Hi, I'm trying to do the Survival Shooter tutorial which requires me to download the assets. I understand that the assets are for unity 4.6, so while importing I was asked to update the files to unity 5, so I did.
I then tried running the saved level to see if everything was working, and for the most part it was. I can move, shoot and the sound works but no enemies spawn, they do show up in the hierarchy but not in the scene. I get this warning when playing,
"Failed to create agent because there is no valid NavMesh
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
CompleteProject.EnemyManager:Spawn() (at Assets/_CompletedAssets/Scripts/Managers/EnemyManager.cs:33)"
I also get this error ""SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.NavMeshAgent:SetDestination(Vector3)
CompleteProject.EnemyMovement:Update() (at Assets/_CompletedAssets/Scripts/Enemy/EnemyMovement.cs:30)"
So the problem is obviously the NavMesh but I don't know how to fix this?
Thanks in advance.
↧
NavMesh Agent not updating (Setting) new path
Im using the NavMesh agent to move my "Inhabitants" around the scene. The desinations are set depending on what time of day it is. There are three destinations at present, a "Home" a "Work" and a "Hobbies" location.
Now the code is getting called, s the debugs and text is being called correctly. BUT the actual pathfinding isnt updating and they stay in the same place even when we move on to another part of the day.. Havny really used NavMesh pathfinding before, so might be simple. But as far as im aware calling the SetDestination method of the NavMesh Agent is all you need to do to update the path.
Heres the code.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// Now using unity built in nav mesh for pathfinding.
public class NavMeshPathfinding : MonoBehaviour {
public enum Destinations{
home,
work,
hobbies
};
Destinations destination;
// The actual destinations in the scene.
public GameObject home;
public GameObject work;
public GameObject hobbies;
public Text destinationText;
NavMeshAgent agent;
private GameObject GM;
private WorldTime worldTime;
// Use this for initialization
void Start () {
agent = GetComponent();
GM = GameObject.FindGameObjectWithTag(Tags.GM);
worldTime = GM.GetComponent();
destinationText.text = "Idle";
}
// Update is called once per frame
void Update () {
// when its a weekday..
if (destination != Destinations.home && (worldTime.timeOfDay == WorldTime.TimeOfDay.Morning || worldTime.timeOfDay == WorldTime.TimeOfDay.Nightime) )
{
destination = Destinations.home;
GetNewDestination();
}
else if (worldTime.day == WorldTime.Days.Saturday || worldTime.day == WorldTime.Days.Sunday)
{
if (destination != Destinations.hobbies && worldTime.timeOfDay == WorldTime.TimeOfDay.WorkTime)
{
destination = Destinations.hobbies;
GetNewDestination();
}
}
else
{
if (destination != Destinations.work && worldTime.timeOfDay == WorldTime.TimeOfDay.WorkTime)
{
destination = Destinations.work;
GetNewDestination();
}
}
}
public void GetNewDestination ()
{
if (destination == Destinations.home)
{
Debug.Log("Heading to" + destination);
agent.SetDestination(home.transform.position);
destinationText.text = "Heading home";
}
else if (destination == Destinations.work)
{
Debug.Log( "Heading too "+ destination);
agent.SetDestination(work.transform.position);
destinationText.text = "Time for work";
}
else if (destination == Destinations.hobbies)
{
Debug.Log("Heading to: "+destination);
agent.SetDestination(hobbies.transform.position);
destinationText.text = "Now for some me time.";
}
}
}
So they head to the inital desination "home" and then stay there..
So what am i doing wrong troops?
Thanks in advance..
↧
Survival Shooter Tutorial: Zombunny Movement Stuck
I brought in the Zombunny assets available for the tutorial and applied the Enemy Movement script supplied with it. When I play my game the Zombunny doesn't follow the player position, but instead walks in place. If I declare a public variable and give the Zombunny a destination of say a mesh in the scene, he will move to that location.
Only the Y position is locked in the Zombunny rigid body.
I bet it's something super simple I missed but I can't find any leads and I'm kinda lost.
This is what the script looks like by the 4th tutorial video:
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour
{
Transform player;
//PlayerHealth playerHealth;
//EnemyHealth enemyHealth;
NavMeshAgent nav;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
//playerHealth = player.GetComponent ();
//enemyHealth = GetComponent ();
nav = GetComponent ();
}
void Update ()
{
//if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
//{
nav.SetDestination (player.position);
//}
//else
//{
// nav.enabled = false;
//}
}
}
↧
NavMeshAgent Different Sizes Help
I am trying to setup 2 different sized NavMeshAgents.
I am setting up grid based building, and I need **NavMeshAgent A** to be able to fit through a gap of **1** on the grid, while **NavMeshAgent B** should only be able to fit through a gap of **2**.
The level is completely flat, and **NavMeshAgent A** places buildings that are **NavMeshObsticles**.
Now, I have read up on this and apparently the only way to achieve this is to setup 2 NavMeshes. 1 with the small details baked into it, and one with the big details, using NavMeshLayers to differentiate them. The problem is, I cannot get 2 NavMeshes working in the same scene. Either they both dynamically set the width to BIG, or SMALL, not both. It seems to only be able to have a single baked setting, which overrides all the others.
So here are my questions:
1. Is what I am trying to do possible with Unity currently?
2. Am I approaching it in the right way?
3. Is setting up 2 different scenes that sync over a network the only way?
↧
↧
Failed to create agent because it is not close enough to the NavMesh
Hello! Well, my NavMeshAgent pops up this error.
While I was trying to fix it, I accidentically cleared my NavMesh and when I press bake again it says exporting tiles and loads a bar but does nothing!
I dont understand what is the problem???
↧
Possible to edit the movement of NavMeshAgent ?
Hello!
I am fiddling with my first pathfinding behaviour. I am using a navmesh and the built in NavMeshAgent and it works great so far!
But I want the agent to take a wider, curved turn (because the agent-object is a vehicle) [Check the illustration below to see what I mean].
If I could edit the navmesh properties at some extend or even create my own navmesh agent, I could say that the agent can only go forward and Slerp the rotation towards the target.
**Does anyone know of a editable NavmeshAgent template code??**
----------------------------------------
Or have any other good tips on how to get the navmesh-agent to do a wider curved vehicle-like turn/rotation?
----------
**What I have tried:**
- A empty gameobject with the unity NavMeshAgent component that goes towards a destination target
- A object with the vehicle model and a script saying it should only go forward and rotate towards the navmeshagent (this creates a nice and wide vehicle-like turn/rotation)
But the problem with this is that vehicle-object goes of the navmesh and collides with obstacles, because it takes a wider turn than the navmeshagent.
----
Illustration showing how the NavmeshAgent turns at the moment VS how I want it to turn:
![alt text][1]
Thanks for any kind of help!
[1]: http://i.imgur.com/JXvNjuH.jpg
↧
NavMeshAgent with Collision - Need Help
Hi,
Since I cannot find a solution for multiple agent sizes operating on a single NavMesh, For the bigger units, I am trying to add colliders on them to prevent them from passing through certain areas they shouldn't be able to fit through.
So to clarify, **I want the path finding to still resolve, but unit to be restricted by a collider.**
I have tried to get this working by adding a box collider, while attaching a rigidBody with Gravity turned off, but this just completely bugs out the path finding. It also sends your character flying if you try place a building with a collider underneath him. Setting the Is Kinematic flag on just makes the unit ignore all other colliders as well.
I realize this solution isn't optimal, but I would like to get the above method working. Am I approaching it in the correct way?
↧
NavMesh agent Pathfinding Zombies
![alt text][1]
[1]: http://oi57.tinypic.com/2m4wpwj.jpg
What i want to do is similiar what in Call of Duty Nazi zombies mode
Player is in house, zombies are going to eat the player but if there is door in zombie's path/way then he have to break that door first and then continue to the player.
I was thinking something like this
Zombie is scanning all doors including empty door holes, then chooses the one what is closest to him and goes that way EVEN if there is empty door hole like 5m longer distance but it always chooses the closest want, thats what i want for that part, but i dont know how i can make this work properly.
I tried to add collider box to doors and stop zombies and i somehow got it work but its not quite right i guess because zombies are still coming a bit through the doors and if player is next to that door zombies start hurting the player because i use simple check like this
if(Distance < 2.0f)
{
ZombieAttackPlayer();
}
I need some help how i can do this system properly, please and thanks!
↧
↧
Navmesh agent problem
I have updated to unity 5, but my AI doesn't follow my player.There was no problem before updated.
I use this
static var player : GameObject;
var target : Transform;
var agent : NavMeshAgent;
function Start(){
target = GameObject.FindWithTag("Player").transform;
player = GameObject.Find("Player");
agent = character.GetComponent.();
}
I use this on function Update
GetComponent.().destination = target.position;
↧
Collider with NavmeshAgent: Is it static?
Believe me, I've been googling this for hours but I have yet to find a conclusive answer.
If I take a GameObject that has **no** Rigidbody attached, but **does** have a NavmesAgent attached, and also add a Collider to it: Does the Collider count as static, or dynamic?
Do I need to add a Rigidbody to make it count as dynamic?
↧
Navmesh Agent velocity VS physics
I'm making an RTS game demo with Navmesh agent. I tried to move many characters to a point at same time. They can collide and avoid each other, also calculate their positions automatically, just like Warcraft.
According to the [document][1]:
> You can use a NavMesh Agent to move e.g. a player character, without physics> Set players agent’s avoidance priority
> to a small number (high priority), to> allow the player to brush through> crowds Move the player agent using> NavMeshAgent.velocity, so that other> agents can predict the player movement> to avoid the player.
My code:
void OnAnimatorMove() {
GetComponent().velocity = agent.desiredVelocity;
}
agent.updatePosition = false; has been already set.
I've also tried to switch on/off of character's "Is Kinematic" in rigidbody. The problem is, if "Is Kinematic" == true, you can not set its velocity since it's not affected by physics engine. If "Is Kinematic" == false, the nav mesh agent will not work correctly since a race condition happened. So, I don't know how to implement "move a character without physics" described in this document. Please help.
P.S. I've made a [demo][2] here with test case for reference, single character movement is fine but multiple characters will cause some issues like nav mesh agent didn't function well after colliding.
[1]: http://docs.unity3d.com/Manual/nav-MixingComponents.html
[2]: https://github.com/reckhou/MobaDemo
↧
Facing angle independent of navmesh agent movement
Hi all,
I am trying to make a character's navmesh movement direction independent of the direction used for other behaviors, like attacking, blocking, casting spells, etc. I have the animations I need, but my game's controls use unity's navmesh agent for movement. From my understanding, a navmesh agent can only move forwards. One can't have the agent walk sideways or backwards or otherwise easily control facing angle without extra code, right? Could I put my navmesh agent in a parent object and just rotate and animate the "character" inside?
End goal example: I click north of my character to get the character moving north, then move my mouse cursor to the east of my character and click an attack hotkey. My character is now facing east, moving north, playing its "strafe left" animation, and playing its fire animation towards the east.
↧
↧
Navmesh agent makes collider rotate on y axis
I have a player character, a knight, that works perfectly. we can make him run arround, block and attack. until now everything is fine. but I also have an orc that uses a navmesh
to make his way to the player when he gets too close. but the problem is that has the enemy gets closer to the player, it starts rotating backwards on the y axis, like he was falling on his back. and when I look into my scene view while playing, I can see that the navmeshagent stays straight, but the capsule collider of the enemy rotates on the y axis.
Here is the Ai script attached to the orc (the enemy is an orc).
using UnityEngine;
using System.Collections;
public class Enemy_ai : MonoBehaviour {
public NavMeshAgent enemy;
public float distance;
public Transform player;
public Transform Enemy;
public Animator anim;
void Start () {
}
void Update () {
transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,transform.eulerAngles.z);
distance = Vector3.Distance (player.position, Enemy.position);
if (distance < 10) {
enemy.SetDestination(player.position);
transform.LookAt(2* transform.position - player.position);
}
}
}
↧
Navmesh Agent skips every other destination
I call GotoPoint() from another script when it's time to send an NPC using Navmesh Agent component to a new destination. Effectively when they reach the destination they stop and wait for another command at a later point.
The problem is the NPC only goes on every *other* call to GotoPoint(). Any idea why it's skipping like this?
internal void GotoPoint(Vector3 dest)
{
paused=false;
animator.SetBool ("paused",paused);
agent.destination = dest;
agent.Resume ();
}
void Update ()
{
if (agent.remainingDistance <= .5 && !paused)
{
paused = true;
animator.SetBool ("paused",paused);
agent.Stop();
}
}
Note: if I add "agent.destination = Vector3.zero;" just after agent.Stop() it seems to then work as expected. But I'd like to know why this is needed.
↧
NavMesh Agent sets rotation to 0,0,0
Hi, I have a Prefab of a sphere collider with a sprite renderer. My camera points straight down so i have the rotation of my prefab is set to x=90 so the sprite faces the camera.
However, after adding a NavMesh Agent and press play, the rotation of my prefab gets set to 0,0,0. my sprite then becomes invisible since the camera is pointing at the side of the sprite.
Any ideas?
thanks
↧