- Home /
Why aren't my parameters for animation not working?
I managed to get my enemy ai script up and working; he detects the player, follows him and shoots when in range. However, I am trying to add parameters to allow animations to play when the enemy has entered different states (attack, follow, e.t.c.) Here is my script with some commented out attempts at making the animations work.
using UnityEngine;
using System.Collections;
public class EnemyAIScript : MonoBehaviour
{
private Transform player;
private float playerDistance;
private float rotationDamping = 5f;
public float attackRange = 10f;
public float moveSpeed = 2f;
public float startChaseRange = 20f;
public static bool isPlayerAlive = true;
public float fireRate = 5f;
private float maxFire;
private float nextFire;
public Rigidbody BulletPrefab;
public float BulletSpeed = 10;
public Vector3 velocity;
Animator animator;
/*AudioSource droneSource;
AudioClip [] droneSounds;
AudioClip droneWalk;
AudioClip droneFire;
AudioClip droneRoll;
AudioClip droneOpen;
AudioClip droneClose;*/
void Start ()
{
player = GameObject.Find ("Player").GetComponent <Transform> ();
animator = GetComponent <Animator> ();
/*droneSounds = GetComponents <AudioClip> ();
for (int i = 0; i < droneSounds.Length; i++)
{
if (droneSounds [i].name == "Fire")
{
droneFire = droneSounds [i];
}
else if (droneSounds [i].name == "Walk")
{
droneWalk = droneSounds [i];
}
else if (droneSounds [i].name == "Roll")
{
droneRoll = droneSounds [i];
}
else if (droneSounds [i].name == "Open")
{
droneOpen = droneSounds [i];
}
else if(droneSounds [i].name == "Close")
{
droneClose = droneSounds [i];
}
}*/
}
void Update ()
{
if (isPlayerAlive)
{
playerDistance = Vector3.Distance (player.position, transform.position);
if (playerDistance < startChaseRange)
{
lookAtPlayer ();
}
if (playerDistance < startChaseRange)
{
if (playerDistance > attackRange)
{
chase ();
}
else if (playerDistance < attackRange)
{
attack ();
}
}
}
}
void lookAtPlayer ()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);
float move = moveSpeed;
animator.SetFloat ("Moving", move);
}
void chase ()
{
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
/*animator.SetTrigger ("Hide");
animator.SetBool ("Open", false);
animator.SetBool ("Moving", false);
animator.SetBool ("Rolling", true);*/
//float move = moveSpeed;
//animator.SetFloat ("Speed", move);
}
void attack ()
{
/*float stop = moveSpeed;
animator.SetFloat ("Stop", stop);
animator.SetBool ("Rolling", false);
animator.SetBool ("Open", true);*/
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Rigidbody instantiatedProjectile = Instantiate (BulletPrefab, GameObject.Find ("spawnPoint").transform.position, Quaternion.identity) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection (new Vector3 (0, 0, BulletSpeed));
/*float shoot = attackRange;
animator.SetFloat ("Shoot", shoot);*/
}
}
}
The audio scripting is on the back burner for now. I'm interested to know why my parameters don't work when I use the animator controller in unity. I set up a parameter, whether a float, int, bool, or trigger, name it correctly to match the script. I then make a transition between two animations in the state machine. Yet when the enemy reaches these values, nothing happens.
For example, the enemy is a spider bot that rolls into a ball (and therefore not humanoid). The animations were made in blender by a friend of mine and are a little trickier to use than animations created in unity but I think I can handle it. The problem starts when he is in idle (just sitting in his ball form). When the player is within 20f of the robot, he moves towards the player. The animation for rolling is supposed to play, but it doesn't. The parameter for movement has been set up and added to the transition, values are fairly low (his move speed is currently 2f).
If anyone could help me or point me to a dedicated animation tutorial (preferably something outside of unity.com itself, those ten minute videos are informative but short.) I am also using unity 5, which may be the reason for it not working (the a.p.i.'s could be different, but for me who is fairly new to coding it is difficult to understand. Any and all help is appreciated.