- Home /
Attack animation Wont Play
My zombie attack animation "swing" wont play, it will play if animation "walk" isn't in the script but other than that it wont play. Please help me.
My Script
using UnityEngine;
using System.Collections;
public class AISimple : MonoBehaviour {
public float Distance;
public Transform Target;
public float lookAtDistance= 50.0f;
public float chaseRange= 20.0f;
public float attackRange= 5.0f;
public float moveSpeed= 1.0f;
public float Damping= 6.0f;
public float fov= 160.0f;
private RaycastHit hit;
public CharacterController controller;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
bool LineOfSight ( Transform target ){
if (Vector3.Angle(target.position - transform.position, transform.forward) <= fov &&
Physics.Linecast(transform.position, target.position, out hit) &&
hit.collider.transform == target) {
return true;
}
return false;
}
/*
This ai will fly and move through objects inlcuding terrain!
*/
void Update (){
if (LineOfSight(Target) == true && hit.transform == Target) // Target is player
{
//enemy sees you - perform some action
// Gauge the distance to the player. Line in 3d space. Draws a line from source to Target.
Distance = Vector3.Distance(Target.position, transform.position);
// Turn yellow enemy is getting close. Will chase soon.
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance < attackRange)
{
attack();
animation.Play ("swing");
}
// Attack! Chase the player until/if player leaves attack range.
if (Distance < chaseRange)
{
chase ();
animation.Play("walk");
}
}
else
{
//enemy doesn't see you
}
}
// Turn to face the player.
void lookAt (){
// Rotate to look at player.
Quaternion rotation= Quaternion.LookRotation(Target.position - transform.position);
// Dampening will slow the turn speed of enemy.
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
// transform.LookAt(Target);
}
void chase (){
//animation.Play("walk");
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
void attack (){
//animation.Stop("walk");
}
}
Answer by RyanZimmerman87 · Feb 10, 2014 at 02:40 AM
It looks like you don't have an else if for either of your animation conditions. Therefor one can easily overwrite the other.
I would set up a bool for when the attack should start and also for when the attack should finish.
You just need to ensure that any one animation cannot interfere with another one unless it's supposed to. For example a death animation would likely overwrite any other logic.
I answered a similar question recently with a fairly detailed example you could start there.
http://answers.unity3d.com/questions/630198/playing-several-animations.html
This doesn't work, can you help me rewrite my code please
using UnityEngine;
using System.Collections;
public class AISimple : $$anonymous$$onoBehaviour {
public float Distance;
public Transform Target;
public float lookAtDistance= 50.0f;
public float chaseRange= 20.0f;
public float attackRange= 5.0f;
public float moveSpeed= 1.0f;
public float Damping= 6.0f;
public float fov= 160.0f;
public bool attackBool;
private RaycastHit hit;
public CharacterController controller;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
bool LineOfSight ( Transform target ){
if (Vector3.Angle(target.position - transform.position, transform.forward) <= fov &&
Physics.Linecast(transform.position, target.position, out hit) &&
hit.collider.transform == target) {
return true;
}
return false;
}
/*
This ai will fly and move through objects inlcuding terrain!
*/
void Start()
{
//set animation Wrap $$anonymous$$ode for the attack to "Once"
animation["swing"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
}
void Update (){
if (LineOfSight(Target) == true && hit.transform == Target) // Target is player
{
//enemy sees you - perform some action
// Gauge the distance to the player. Line in 3d space. Draws a line from source to Target.
Distance = Vector3.Distance(Target.position, transform.position);
// Turn yellow enemy is getting close. Will chase soon.
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance < attackRange)
{
attack();
attackBool = true;
}
// Attack! Chase the player until/if player leaves attack range.
if (Distance < chaseRange)
{
chase ();
//attackBool = false;
}
}
else
{
//enemy doesn't see you
}
if (attackBool == true) {
animation.Play ("swing");
} else {
animation.Stop ("swing");
}
}
// Turn to face the player.
void lookAt (){
// Rotate to look at player.
Quaternion rotation= Quaternion.LookRotation(Target.position - transform.position);
// Dampening will slow the turn speed of enemy.
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
// transform.LookAt(Target);
}
void chase (){
//animation.Play("walk");
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
void attack (){
//animation.Stop("walk");
}
}
Your answer
Follow this Question
Related Questions
Animation won't start when collision starts 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Character falls through the ground 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can someone help me with my script? 0 Answers