- Home /
Question by
RubiixDev · Aug 17, 2020 at 04:25 PM ·
animationsclick to moveclicktomove
Character keeps moving after reaching target location, How can i solve it?
Hi, im trying to make a click to move character controller but now im struggling a bit with the animations. If i turn off the animatior the character moves perfectly but when i turn them on weird things happen. - In some cases the character keeps waliking in circles endlesly. - Every time it reaches the location the character rotates in the y axis. Thats my code:
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.AI;
public class MovimentTerceraPersona : MonoBehaviour
{
NavMeshAgent agent;
Animator playerAnimator;
bool pRunning = false;
public Camera cam;
void Start()
{
playerAnimator = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
RaycastHit hit;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit, 100))
{
agent.destination = hit.point;
pRunning = true;
}
}
if (agent.remainingDistance <= agent.stoppingDistance)
{
pRunning = false;
}
else
{
// pRunning = true;
}
playerAnimator.SetBool("running", pRunning);
float toStop = agent.remainingDistance;
Debug.Log(toStop );
}
}
Comment
Your answer
Follow this Question
Related Questions
Moving NavMeshAgent inside a Collider 2 Answers
Player won't move to a certain location clicked by the mouse 0 Answers
How do I use SpriteManager? 1 Answer