- Home /
Click to move, navAgent and mecanim = animation playing too early
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
public class ClickToPickup : MonoBehaviour
{
private Animator anim;
private NavMeshAgent farmBoy;
private bool walking;
private bool piggyClicked;
void Awake()
{
anim = GetComponent<Animator>();
farmBoy = GetComponent<NavMeshAgent>();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
{
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.collider.tag == ("Piggy"))
{
piggyClicked = true;
farmBoy.destination = hit.transform.position;
}
else
{
piggyClicked = false;
farmBoy.destination = hit.point;
// farmBoy.Resume();
}
}
}
if (farmBoy.remainingDistance <= farmBoy.stoppingDistance)
{
walking = false;
}
else
{
walking = true;
}
anim.SetBool("isWalking", walking);
if (piggyClicked)
{
PickUp();
}
}
void PickUp()
{
anim.SetBool("pickupPiggy", true);
anim.SetBool("isHolding", true);
}
}
Greetings.. so the the setup i have is 6 animations Idle, Run, PickupPiggy, HoldPiggy(idle), CarryPiggy, PutdownPiggy.
farmBoy is supposed to run anywhere i Left click on the ground(plane) and if i Left click on a piggy he should look at the pig and run to it then do the PickupPiggy animation and go into the HoldPiggy Idle state and when i click on the ground now he should play the CarryPiggy animation until i click on the spot i want to drop the piggys off at
i havent tried to parent the pigs to my guy yet until i can get the animations working
i used the script from the unity tutorial Click to Move and mostly its working when i click on the ground plane farmboy plays the Run animation and then stops at the spot i clicked and goes into Idle just fine, but when i click on a piggy farmBoy does the pickup animation right away and then does the carry animation to the piggy and goes into HoldPiggy idle animation like he should, i need some help please..
everything seems to be working except he plays the PickupPiggy animation before he gets to the piggy once thats all working ill need to figure out how to drop the pigs off in the pen a spot on the map
other issues that are coming soon are the distance to the pigs is wrong he runs right on top of em instead of next to them.. the speed when he is carrying a pig needs to be slower than when hes not holding one
any help to why it plays the animation first before running to the pig would be wonderful
ive tried if statements in the void PickUp()
" if (farmBoy.remainingDistance <= farmBoy.stoppingDistance) { anim.SetBool("pickupPiggy", true); anim.SetBool("isHolding", true); } " but it didnt change anything
btw i can click the bools in the animator and he pickups and putsdown and carrys just the way i want
thanks
Your answer
Follow this Question
Related Questions
object position changed in play mode after adding animation to animator component 0 Answers
I can't setting at once ModelImporter.sourceAvatar in script. 1 Answer
Any idea why my animation isn't working? 0 Answers
Multiple NavmeshAgents waiting each other to move to destination?? 1 Answer
Mecanim Example Scenes import warnings. 0 Answers