Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by shahab · Feb 20, 2017 at 07:41 PM · animationagent

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";
             }
         }
 
 
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges