Question by
baoyangchen · Aug 28, 2017 at 11:33 AM ·
animationnavmeshagent
Animation with Nav Mesh Agent
Dear all-
I ran into this problem below, really appericated if you can help-
I set up a randomly move character with nav mesh agent, and I want to add animation to it.
I have animations ready "walk" and "idle", and how can I apply "walk" to the character when it's moving, and "idle" when it's stop.
Thank you again,
the code I use for randomly move is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class moverandomly : MonoBehaviour {
public float timer;
public int newtarget;
public float speed;
public NavMeshAgent nav;
public Vector3 Target;
// Use this for initialization
void Start () {
nav = gameObject.GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
nav.speed = speed;
timer += Time.deltaTime;
if(timer >= newtarget)
{
newTarget();
timer = 0;
}
}
void newTarget ()
{
float myX = gameObject.transform.position.x;
float myZ = gameObject.transform.position.z;
float xPos = myX + Random.Range(myX - 100, myX + 100);
float zPos = myZ + Random.Range(myZ - 100, myZ + 100);
Target = new Vector3(xPos,gameObject.transform.position.y,zPos);
nav.SetDestination(Target);
}
}
Comment
Your answer
Follow this Question
Related Questions
Character Controller moves on top of Rigidbody after Animation 0 Answers
I need the Nav Mesh Stop , I need the Animation to Play Javascript 0 Answers
NavmeshAgent rotating and moving with root motion animations 0 Answers
EnemyAI movement stop if it has enable animator controller 1 Answer
Blend Trees with NavMeshAgent 0 Answers