how to make an agent avoid doing animation not in its destination place rapidly?
Hello, dear unity users. I'm new to this section so if I made any mistake forgive me :D. I want to clarify my question with a video attached to my post. In my scene, the agent goes toward its destination and when OnTrigger happens it runs sit animation. As you can see when the agent wants to sit it change its position to fit the agent destination. I want the agent to sit on the box and stop updating its position and do the same for every animation.Please help me with this matter, thank you. here is the link to the video: link text
here is my code:
public class NPC : MonoBehaviour {
public bool running = true;
public float inputHoldDelay = 4f;
public float distance;
public float speedDampTime = 0.1f;
public Transform firstDestination;
public string status;
private NavMeshAgent agent;
private ListofLocations locations;
private ListofAnimations animations;
private Animator animator;
private GameObject[] POI;
private WaitForSeconds inputHoldWait;
private Vector3 destinationPosition;
private bool trigger;
private int steps = 0;
private float Wait = 2f;
private bool sit = true;
private const float stopDistanceProportion = 0.1f;
private readonly int hashSpeedPara = Animator.StringToHash("Speed");
void Start () {
animator = GetComponent<Animator>();
locations = GetComponent<ListofLocations>();
animations = GetComponent<ListofAnimations>();
agent = GetComponent<NavMeshAgent>();
inputHoldWait = new WaitForSeconds(Wait);
}
private void OnAnimatiorMove() {
agent.velocity = animator.deltaPosition / Time.deltaTime;
}
void Update () {
CheckDistance();
CheckAnimationState();
}
private IEnumerator waitForNextPosition() {
if(status == "sitting")
{
yield return new WaitForSeconds(Wait);
animator.SetTrigger("SitToStand");
Wait = 3;
yield return new WaitForSeconds(Wait);
steps += 1;
if (steps == locations.interactableLocation.Length)
{
steps = 0;
}
}
else
{
yield return new WaitForSeconds(Wait);
steps += 1;
if (steps == locations.interactableLocation.Length)
{
steps = 0;
}
}
}
private void CheckDistance() {
agent.destination = locations.interactableLocation[steps].position;
distance = Vector3.Distance(agent.destination, agent.transform.position);
if (running == true)
{
Running();
}
if (running == false)
{
Walking();
}
}
private void Running() {
if (distance >= 5)
{
animator.SetFloat(hashSpeedPara, 1.6f, speedDampTime, Time.deltaTime);
agent.speed = 3f;
agent.updateRotation = true;
trigger = true;
}
if (distance < 5 && distance > 0.2)
{
animator.SetFloat(hashSpeedPara, 1f, speedDampTime, Time.deltaTime);
agent.speed = 2f;
agent.updateRotation = true;
trigger = true;
}
if (distance <= 0.2)
{
animator.SetFloat(hashSpeedPara, 0, speedDampTime, Time.deltaTime);
agent.updateRotation = false;
if (status == "sitting")
{
Quaternion targetRotation = locations.interactableLocation[steps].rotation;
Quaternion targetRotationInverse = Quaternion.Inverse(targetRotation);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotationInverse, 0.1f);
}
else
{
Quaternion targetRotation = locations.interactableLocation[steps].rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 0.1f);
}
}
}
private void Walking()
{
if (distance > 0.2)
{
animator.SetFloat(hashSpeedPara, 1f, speedDampTime, Time.deltaTime);
agent.speed = 2f;
agent.updateRotation = true;
trigger = true;
}
if (distance <= 0.2)
{
animator.SetFloat(hashSpeedPara, 0, speedDampTime, Time.deltaTime);
agent.updateRotation = false;
Quaternion targetRotation = locations.interactableLocation[steps].rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 0.1f);
}
}
private void CheckAnimationState() {
if (agent.destination == transform.position && trigger == true)
{
switch (steps)
{
case 0:
animator.SetTrigger("Sitting");
Wait = animations.animations[steps].length;
break;
case 1:
animator.SetTrigger("Tut");
Wait = animations.animations[steps].length;
break;
case 2:
animator.SetTrigger("Booty");
Wait = animations.animations[steps].length;
break;
case 3:
animator.SetTrigger("Arms");
Wait = animations.animations[steps].length;
break;
case 4:
animator.SetTrigger("Slide");
Wait = animations.animations[steps].length;
break;
default:
steps = 0;
break;
}
trigger = false;
StartCoroutine(waitForNextPosition());
}
}
void OnTriggerEnter(Collider other)
{
PointsofInterest poi = other.GetComponent<PointsofInterest>();
if (poi)
{
if (poi.sittingPosition == true)
{
status = "sitting";
}
if (poi.actingPosition == true)
{
status = "acting";
}
if (poi.speakingPosition == true)
{
status = "speaking";
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612111452im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player animation unresponsive? 0 Answers
Animation ParentConstraint Freeze settings at runtime 2 Answers
Animating Material, how to set material after animation? 0 Answers
Can't add animated properties to a 3D character in 2018 0 Answers
Prevent Snapping from Custom Bone Rotation to Animation 0 Answers