- Home /
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.
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.
It does. It has the Animation component attached and the correct animations. No errors are given either.
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 :/
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.