- Home /
How do I play the unity free walk animation on raycast(click) and continue to play until the character has arrived at the raycast destination?
How do I play the unity free walk animation on raycast(click) and continue to play until the character has arrived at the raycast destination?
More or less what is says in the question bar, I'm trying to play a walk animation on my character when I click in the game, I make him move using Ray, Raycasthit and Raycast, can anyone help? The animation plays when but stops once I've clicked, it doesn't play for the duration of his movement.
Also I'm so sorry for the screenshot it's my first question. If someone could guide me as to how I share my script, I'd happily do that.
NavMeshAgent playerAgent;
bool isMoving = false;
void Start()
{
playerAgent = GetComponent<NavMeshAgent> ();
}
void Update ()
{
if (Input.GetMouseButtonDown (0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
GetInteraction ();
}
void GetInteraction ()
{
Ray interactionRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit interactionInfo;
if(Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
{
//Interacted object is the object that we hit if we hit something, if we didn't hit it is null
GameObject interactedObject = interactionInfo.collider.gameObject;
//what we hit, what to do with it via tag interactable or not
if (interactedObject.tag == "Interactable Object") {
Debug.Log ("Interactable Interacted");
} else {
isMoving = true;
//stop playing walking through objects, set up pathfinding
playerAgent.destination = interactionInfo.point;
if (isMoving)
{
GetComponent<Animator> ().Play ("Walk");
}
}
}
}
Comment