Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
3
Question by sqallpl · Oct 23, 2014 at 05:32 AM · c#movementanimatornavmeshagent

Root motion handled by script. How to still use root motion with disabled navmesh agent?

I have this script from one of Unity's tutorials with locomotion for mecanim. Everything works fine and Mecanim is working with NavMeshAgent. The thing is I would like to disable NavMeshAgent from time to time and still use root motion (for example when a character want to sit down on a chair that is an obstacle or it's baked into navmesh).

At this moment, when I disable NavMeshAgent component my animations are played without root motion. Root motion is automatically set to "handled by script" and it just won't use root motion when the NavMeshAgent is disabled.

Do you have an idea how to modify this script so I will still get the root motion from animations even if the NavMeshAgent is disabled?

 using UnityEngine;
     using System.Collections;
     
     public class Agent : MonoBehaviour {
         
         public GameObject            particle;
         protected NavMeshAgent        agent;
         protected Animator            animator;
         
         protected Locomotion locomotion;
         protected Object particleClone;
         
         
         // Use this for initialization
         void Start () {
             agent = GetComponent<NavMeshAgent>();
             agent.updateRotation = false;
             
             animator = GetComponent<Animator>();
             locomotion = new Locomotion(animator);
             
             particleClone = null;
         }
         
         protected void SetDestination()
         {
             // Construct a ray from the current mouse coordinates
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit = new RaycastHit();
             if (Physics.Raycast(ray, out hit))
             {
                 if (particleClone != null)
                 {
                     GameObject.Destroy(particleClone);
                     particleClone = null;
                 }
                 
                 // Create a particle if hit
                 Quaternion q = new Quaternion();
                 q.SetLookRotation(hit.normal, Vector3.forward);
                 particleClone = Instantiate(particle, hit.point, q);
                 
                 agent.destination = hit.point;
             }
         }
         
         protected void SetupAgentLocomotion()
         {
             if (AgentDone())
             {
                 locomotion.Do(0, 0);
                 if (particleClone != null)
                 {
                     GameObject.Destroy(particleClone);
                     particleClone = null;
                 }
             }
             else
             {
                 float speed = agent.desiredVelocity.magnitude;
                 
                 Vector3 velocity = Quaternion.Inverse(transform.rotation) * agent.desiredVelocity;
                 
                 float angle = Mathf.Atan2(velocity.x, velocity.z) * 180.0f / 3.14159f;
                 
                 locomotion.Do(speed, angle);
             }
         }
         
         void OnAnimatorMove()
         {
             agent.velocity = animator.deltaPosition / Time.deltaTime;
             transform.rotation = animator.rootRotation;
         }
         
         protected bool AgentDone()
         {
             return !agent.pathPending && AgentStopping();
         }
         
         protected bool AgentStopping()
         {
             return agent.remainingDistance <= agent.stoppingDistance;
         }
         
         // Update is called once per frame
         void Update () 
         {
             if (Input.GetButtonDown ("Fire1")) 
                 SetDestination();
             
             SetupAgentLocomotion();
         }
     }
     
 
Comment
Add comment · Show 2
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
avatar image RJproz · Aug 15, 2015 at 01:58 PM 0
Share

Did you got any solution?

avatar image sqallpl RJproz · Nov 18, 2015 at 02:17 PM 0
Share

I've used some temporary workaround (but I don't know if I would be using it in the future) by adding 'offline' bool to the script and not doing some things in the script when this bool is true. When I need to disable Nav$$anonymous$$eshAgent and use root motion from animator I set this bool to true and disable Nav$$anonymous$$eshAgent. I don't know if its clean way of doing that anyway. I'm using different script than the one provided in the question btw.

Did you found any other solution?

 using UnityEngine;
 using System.Collections;
 
 public class Agent2 : $$anonymous$$onoBehaviour {
     
     
     protected Nav$$anonymous$$eshAgent        agent;
     protected Animator            animator;
     public bool offline = false;
     
     protected Locomotion locomotion;
     
     
     
     
     // Use this for initialization
     void Start () {
         agent = GetComponent<Nav$$anonymous$$eshAgent>();
         agent.updateRotation = false;
         agent.updatePosition = true;
         
         animator = GetComponent<Animator>();
         locomotion = new Locomotion(animator);
         
         
     }
     
     
     
     protected void SetupAgentLocomotion()
     {
         if (AgentDone())
         {
             locomotion.Do(0, 0);
             
         }
         else
         {
             float speed = agent.desiredVelocity.magnitude;
             
             Vector3 velocity = Quaternion.Inverse(transform.rotation) * agent.desiredVelocity;
             
             float angle = $$anonymous$$athf.Atan2(velocity.x, velocity.z) * 180.0f / 3.14159f;
             
             locomotion.Do(speed, angle);
         }
     }
     
     
     void OnAnimator$$anonymous$$ove()
     {
         agent.velocity = animator.deltaPosition / Time.deltaTime;
 
 
 
         transform.rotation = animator.rootRotation;
         if (offline == true){
         transform.position = animator.rootPosition;
         }
     }
 
 
     
      public bool AgentDone()
     {
         if (offline == false){
         return !agent.pathPending && AgentStopping();
         }
         else {
             return true;
         }
     }
 
     public bool AgentStopping()
     {
         if (offline == false){
         return agent.remainingDistance <= agent.stoppingDistance;
         }
         else {
             return false;
         }
     }
 
     
     // Update is called once per frame
     void Update () 
     {    
         if (offline == false){
         SetupAgentLocomotion();
         }
     }
 
     public void SetOffline()
     {
          offline = true;
     }
     public void SetOnline()
     {
         offline = false;
     }
 }
 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Incremental Move and stop 1 Answer

Problem getting player to switch navmesh layers 1 Answer

Finding the axis of a curving pipe 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