Stuck with navmesh

Hi guys im trying to implemente the navmesh in my game that work fine without it, only some problems with stucks in wall, so i decide implemente it. I create the navemesh with no problem and now i change my script to the button one. The problem is now i click to move but my character just stay stuck in initial position running in same place. Can anyone help me if possible?

using UnityEngine;
using System.Collections;

public class ClickToMove : MonoBehaviour
{
    public float speed;
    public CharacterController controller;
    private Vector3 position;
   
    public AnimationClip run;
    public AnimationClip idle;
   
    public static bool attack;
    public static bool die;
   
    public static Vector3 cursorPosition;

    NavMeshAgent agent;
   
    // Use this for initialization
    void Start ()
    {
        position = transform.position;
        agent = GetComponent<NavMeshAgent> ();
    }
   
    // Update is called once per frame
    void Update ()
    {
        locateCursor ();
        if (!attack && !die)
        {
            if (Input.GetMouseButton (0))
            {
                localPosition ();   
            }
           
            moveToPosition ();
        }
        else
        {
           
        }
    }
   
    void localPosition()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
       
        if (Physics.Raycast (ray, out hit, 1000))
        {
            if(hit.collider.tag!="Player"&&hit.collider.tag!="Enemy")
            {
                agent.destination=hit.point;
            }
        }
    }
   
    void locateCursor()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
       
        if (Physics.Raycast (ray, out hit, 1000))
        {
            cursorPosition = hit.point;
        }
    }
   
    void moveToPosition()
    {
        if (Vector3.Distance (transform.position, position) > 1)
        {
            Quaternion newRotation = Quaternion.LookRotation (position - transform.position, Vector3.forward);   
           
            newRotation.x = 0f;
            newRotation.z = 0f;
           
            transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * 10);
            controller.SimpleMove (transform.forward * speed);
           
            animation.CrossFade (run.name);
        }
        else
        {
            animation.CrossFade(idle.name);   
        }
    }
}

Anyone plz? I taking a bad time try to figure this out cuz if i take of the navmesh the player moves fine with all animations working 100% when i implement the navmesh the animation simply stop work properly… Appreciate any kind of information

Edit: in line 56 i hv - animation.CrossFade(run.name);

When you bake a nav mesh you can select the layers you want to include and ones you don’t.
I would look at that.

[quote=“forbidden, post:3, topic: 557049, username:forbidden”]
When you bake a nav mesh you can select the layers you want to include and ones you don’t.
I would look at that.
[/quote]
I hv my environemtn set to Ground layer and bake them has u can see in pic down

Im getting an error now when i click play said “Set Destination” can only be called on an active agent thas has been placed on a NaveMesh. Faile create agent because invalid NavMesh…

Õk i fix error just rebake i hv an error… but the problem keeps so i click to move and character just move in the same position side to side… dont go where i click. the animation is working now but the character dont stop moving and dont go anywhere… just seems to be stuck

Maybe uncheck use root motion on your animater controller if you have one.

Scratch that I seen your screen shots and your not using locomotion.

I would maybe check the colliders on your character and brake them up to prevent them from possible interfering with one another.

The only thing i hv in my character is character controller and the navmesh… i dont hv any more colliders on it… Could character controller interfear with navmesh? Another thing i see is the character is above the ground, when i hit play i goes inside collider and back out in each step… i thinks thats it that stuck the character but why is it doing that if without navmesh it works fine?

with game stopped / when it play


I just tried added a character controller to a character and it moved no problem if it was the only colider on it.
Once I added another collider with the character controller it started bouncing all over the place.
I also tried puting coliders under the parent game object as their own emoty gameobeject child and it also caused problems with the nav agent as well… bouncing all oer.

Now if I needed more colliders then I would just have put that same empty game object under my main one and change the layer of the child to something outside the nav mesh then it would work fine with more then one colider.

It keep stucking… If i cant figure this out i will hv to take out navmesh and reduce the enmies range so they onlyn attack when im near and then go stuck the walls… but i reallty would like to put game working fine…

Please post your current movement script and a screenshot of the character’s inspector view.

Also, check all of forbidden’s excellent suggestions, and for now make sure your character only has one collider, including children.

Hi, the move script is the down one and the character the one in image i take from asset store just for now until i made my own. I check all childs of character even weapon and only hv the character controler and navmesh in their. Thx for ur help

    using UnityEngine;
    using System.Collections;
    
    public class ClickToMove : MonoBehaviour
    {
        public float speed;
        public CharacterController controller;
        private Vector3 position;
      
        public AnimationClip run;
        public AnimationClip idle;
      
        public static bool attack;
        public static bool die;
      
        public static Vector3 cursorPosition;
    
        NavMeshAgent agent;
      
        // Use this for initialization
        void Start ()
        {
            position = transform.position;
            agent = GetComponent<NavMeshAgent> ();
        }
      
        // Update is called once per frame
        void Update ()
        {
            locateCursor ();
            if (!attack && !die)
            {
                if (Input.GetMouseButton (0))
                {
                    localPosition (); 
                }
              
                moveToPosition ();
            }
            else
            {
              
            }
        }
      
        void localPosition()
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
          
            if (Physics.Raycast (ray, out hit, 1000))
            {
                if(hit.collider.tag!="Player"&&hit.collider.tag!="Enemy")
                {
                    agent.destination=hit.point;
                }
            }
        }
      
        void locateCursor()
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
          
            if (Physics.Raycast (ray, out hit, 1000))
            {
                cursorPosition = hit.point;
            }
        }
      
        void moveToPosition()
        {
            if (Vector3.Distance (transform.position, position) > 1)
            {
                Quaternion newRotation = Quaternion.LookRotation (position - transform.position, Vector3.forward); 
              
                newRotation.x = 0f;
                newRotation.z = 0f;
              
                transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * 10);
                controller.SimpleMove (transform.forward * speed);
              
                animation.CrossFade (run.name);
            }
            else
            {
                animation.CrossFade(idle.name); 
            }
        }
    }

  1. Initialize the values of attack and die:
public static bool attack = false;
public static bool die = false;

Also, should they really be static?

  1. In moveToPosition(), use agent.destination, not position (which is the player’s starting position):
void moveToPosition()
{
    if (Vector3.Distance (transform.position, agent.destination) > 1)
    {
        ...

So in moveToPosition its suppose to use this? Cuz it gets me and error cant use hit.position. And yap shoudnt be static :stuck_out_tongue: the little stupid things…

    void moveToPosition()
    {
        if (Vector3.Distance (transform.position, position) > 1)
        {
            Quaternion newRotation = Quaternion.LookRotation (position - transform.position, Vector3.forward);   
           
            newRotation.x = 0f;
            newRotation.z = 0f;
           
            transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * 10);

            agent.SetDestination(hit.point);
            animation.Play(run.name);
        }

Try this:

void moveToPosition()
{
  if (Vector3.Distance (transform.position, agent.destination) > 1)
  {
    Quaternion newRotation = Quaternion.LookRotation (agent.destination - transform.position, Vector3.forward);
 
    newRotation.x = 0f;
    newRotation.z = 0f;
 
    transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * 10);
    controller.SimpleMove (transform.forward * speed);
 
    animation.CrossFade (run.name);
  }
  else
  {
    animation.CrossFade(idle.name);
  }
}

I used agent.destination instead of position.

It works, the only issue now is when i click to move initially the character slide a bit and when arrive the location it rotates a bit before stop… But at least it works… Now i hv to implement this in my enemies… ty Tony if u possible know why is it doing that things i will appreciate other way their will be no problem… could be any animation issue or something…

  1. For the rotation, let the NavMeshAgent handle it. Don’t adjust rotation in moveToPosition().

  2. For the speed, try something like this:

controller.SimpleMove (transform.forward * agent.velocity.magnitude * Time.deltaTime);

so just remove rotation from movetoposition()? i will take a shot… i probably will need only one more help from u :stuck_out_tongue: but i hope is the last one. Can u help me with the enemy script about the navmesh if possible cuz i try do something like in player but dont work at all…
this is the script. i will try not take more of ur time… :s

using UnityEngine;
using System.Collections;

public class Mob : MonoBehaviour
{
    public float speed;
    public float attackRange;
    public float chaseRange;
   
    public CharacterController controller;
    public Transform player;
    public LevelSystem playerLevel;
    private Fighter opponent;
   
    public AnimationClip attackClip;
    public AnimationClip run;
    public AnimationClip idle;
    public AnimationClip die;
   
    public double impactTime = 0.36;
   
    public int maxHealth;
    public int  health;
    public int damage;
   
    public int xp;
   
    private bool impacted;
   
    private int stunTime;
   
    // Use this for initialization
    void Start ()
    {
        health = maxHealth;
        opponent = player.GetComponent<Fighter> ();
    }
   
    // Update is called once per frame
    void Update ()
    {
        if (!isDead ())
        {
            if(stunTime<=0)
            {
                if (inChaseRange())
                {
                    if (!inRange ())
                    {
                        chase ();
                    }
                    else
                    {
                        animation.Play(attackClip.name);
                        attack();
                       
                        if(animation[attackClip.name].time>0.9*animation[attackClip.name].length)
                        {
                            impacted = false;
                        }
                    }
                }
                else
                {
                    animation.Play(idle.name);
                }
            }
            else
            {
               
            }
        }
        else
        {
            dieMethod();
        }
    }
   
    void attack()
    {
        if (animation [attackClip.name].time > animation [attackClip.name].length * impactTime&&!impacted&&animation[attackClip.name].time<0.9*animation[attackClip.name].length)
        {
            opponent.getHit(damage);
            impacted = true;
        }
    }
   
    bool inRange()
    {
        if(Vector3.Distance(transform.position, player.position)<attackRange)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
    bool inChaseRange()
    {
        if(Vector3.Distance(transform.position, player.position)<chaseRange)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
   
    public void getHit(double damage)
    {
        health = health - (int)damage;
       
        if(health<0)
        {
            health = 0;
        }
    }
   
    public void getStun(int seconds)
    {
        CancelInvoke("stunCountDown");
        stunTime = seconds;
        InvokeRepeating("stunCountDown", 0f, 1f);
    }
   
    void stunCountDown()
    {
        stunTime = stunTime - 1;
       
        if(stunTime==0)
        {
            CancelInvoke("stunCountDown");
        }
    }
   
   
    void chase()
    {
        transform.LookAt(player.position);
        controller.SimpleMove(transform.forward*speed);
        animation.CrossFade(run.name);
    }
   
    void dieMethod()
    {
        animation.Play (die.name);
       
        if(animation[die.name].time>animation[die.name].length*0.9)
        {
            playerLevel.exp =     playerLevel.exp + xp;
            Destroy(gameObject);
        }
    }
   
    bool isDead()
    {
        if (health <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
    void OnMouseOver()
    {
        player.GetComponent<Fighter>().opponent = gameObject;
    }
}

Play around with your enemy script and try to make it work the same way as your player script.