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
0
Question by matteonite · Feb 17, 2015 at 06:39 PM · c#animationaipathanimation editor

Animations not working

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(NetworkView))]
 public class WalkPath : MonoBehaviour {
     public GameObject[] points;
     public int onRouteTo;
     public string idleAnim = "idle1";
     public string walkAnim = "walk";
     private GameObject me;
     public Transform target;
     public int moveSpeed = 2;
     public int rotationSpeed = 3;
     private Transform myTransform; 
     public int maxDistance;
     private float footTimer = 0;
     private CharacterController _cc;
     private bool halted = false;
     private float haltTime;
     private string currentAnim = "walk";
     public string tag;
     private string lastAnim;
 
     // Use this for initialization
     void Start () {
         me = this.gameObject;
 
         if(Network.peerType == NetworkPeerType.Server){
             onRouteTo = 0;
             points = GameObject.FindGameObjectsWithTag(tag);
 
             target = points[onRouteTo].transform;
             _cc = GetComponent<CharacterController>();
             
             maxDistance = 1;
             myTransform = transform;
         }
     }
 
     void OnEnable(){
         Messenger.AddListener ("Halt", Halt);
     }
 
     void OnDisable(){
         Messenger.RemoveListener ("Halt", Halt);
     }
 
     private void Halt(){
         if(Network.peerType == NetworkPeerType.Server){
             halted = true;
             haltTime = 2;
         }
     }
 
     // Update is called once per frame
     void Update () {
         PlayAnim ();
 
         if(Network.peerType == NetworkPeerType.Server){
             //look at target
             myTransform.rotation= Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);    
         
             if(Vector3.Distance(target.position, myTransform.position) > maxDistance){        
                 if(!halted){
                     _cc.SimpleMove(myTransform.TransformDirection(Vector3.forward) * 2);
                     WalkAnim();
                 }
             }
             
             if(Vector3.Distance(target.position, myTransform.position) <= maxDistance){        
                 //Move towards Target(Player)
                 onRouteTo++;
                 
                 if(onRouteTo > points.Length)
                     onRouteTo = 0;
                     
                 target = points[onRouteTo].transform;
             }
             
             if(_cc.isGrounded == false){
                 _cc.SimpleMove(myTransform.TransformDirection(Vector3.down) * 1);
             }
 
             if(haltTime > 0){
                 haltTime -= Time.deltaTime;
                 IdleAnim();
 
                 if(haltTime <= 0){
                     halted = false;
                     haltTime = 0;
                 }
             }
         }
 
     }
 
     void PlayAnim(){
         me.animation.CrossFade(currentAnim);
         //if(lastAnim != currentAnim){
         //    Messenger<string>.Broadcast("PlayAnim", currentAnim);
         //    lastAnim = currentAnim;
         //}
     }
 
     void IdleAnim(){
         networkView.RPC("SyncAnim", RPCMode.All, idleAnim);
     }
     
     void WalkAnim(){
         networkView.RPC("SyncAnim", RPCMode.All, walkAnim);
     }
 
     [RPC]
     void SyncAnim(string theAnim){
         currentAnim = theAnim;
     }
 }
 

I have a script set up so a NPC (That is spawned in with another script when the game is a server) walks on a set path around a town. However Animation.crossfade doesn't work, and niether does animation.Play.

I debugged it a bit and I know the PlayAnim is being played I've moved where its being called a bit and it doesn't work, and I know that the script is trying to run the correct anim so I'm not sure what to do.

Thanks in advance for help.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kerihobo · Feb 18, 2015 at 01:08 AM

Does your NPC have an animation component?

I think it needs one if you're using animation.play or .crossfade.

It must have those animations attached to it.

Comment
Add comment · Show 3 · Share
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 matteonite · Feb 18, 2015 at 02:07 AM 0
Share

It does. It has the Animation component attached and the correct animations. No errors are given either.

avatar image Kerihobo · Feb 18, 2015 at 02:18 AM 0
Share

gee, confusing. Well, judging from your script... I'd say IdleAnim() should be playing. put a debug.Log(); in there and double check that it's firing. If it is, then the problem is in your line

networkView.RPC("SyncAnim", RPC$$anonymous$$ode.All, idleAnim);

I can't see what that script looks like, but if yoyr log works, then it means the problem is in that script.

I dont know what you're doing to play the animation, I'm only familiar with like... RPC.animation.play ("rpc_idle");

That's all I got sorry :/

avatar image Kerihobo · Feb 18, 2015 at 02:25 AM 0
Share

just for kicks... you could make yourself VERY sure you have the right animations by changing

 public string idleAnim = "idle1";
 public string walkAnim = "walk";

to

 public AnimationClip idleAnim;
 public AnimationClip walkAnim;

and set them in the inspector ins$$anonymous$$d by drag dropping them from your project pane.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy AI acting strangely and only animating on the first placed in scene. 1 Answer

Distribute terrain in zones 3 Answers

AI Animation problems 1 Answer

Coroutine to WaitForSeconds() on enemy AI causing all instances of AI to pause. Solution? 1 Answer

How do I get my patrolling enemies to go back to the patrol point they had not gotten to because they chased the player? 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