How to animate and then stop animation after reaching destination in a character with navmesh agent?
I just made the script to play the running animation of my character but now i want to it to stop when it reaches it's destination as any point i click, it should play the idle animation or stop the running one, here's the script:
using UnityEngine; using System.Collections;
public class clickToMove : MonoBehaviour { UnityEngine.AI.NavMeshAgent navAgent; Animator anim; private bool isMoving; private Vector3 targetPoint;
// Use this for initialization
void Start()
{
navAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
anim = GetComponent<Animator>();
isMoving = false;
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) // 0 is Left-Click, 1 is Right-Click
{
if (Physics.Raycast(ray, out hit, 100))
{
navAgent.SetDestination(hit.point);
isMoving = true;
}
}
if (isMoving == true)
{
anim.Play("Correr");
}
if (transform.position == targetPoint)
{
isMoving = false;
anim.Play("Quieto");
}
}
}
How can it be done?
Answer by PrettyLyn · Sep 05, 2018 at 02:06 PM
This is the code I used hope it helps... @Unidesky
if (Input.GetMouseButtonUp (0))
{
if (Physics.Raycast (ray, out hit, 100))
{
walking = true;
agent.destination = hit.point;
}
}
if (agent.remainingDistance <= agent.stoppingDistance)
{
walking = false;
}
else
{
walking = true;
}
anim.SetBool ("isWalking", walking);
Thank you so much for this, I've been trying for the last hour and a half to accomplish this!
Your answer
![](https://koobas.hobune.stream/wayback/20220612131505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animation with Nav Mesh Agent 0 Answers
Blend Trees with NavMeshAgent 0 Answers
Rigidbodies stopping Root Motion of animations 0 Answers
Making your ai attack then return to the navmesh when player leaves trigger 0 Answers
How do I get my guard(s) to stop at NavPoint(s) and go into IdleState in C# 0 Answers