I have an EnemyScript, that lets my animated Enemy target and chase the player from certain distances and returns to a set Startpoint, if the Playerdistance is to high.
Everything works fine(more or less), but now i want my enemy to "die"(eg play a certain Animation) if he is hit with the weapon.(hit Once, no facy health scripts, just a one hitter).
I Tried OnCollisionEnter with a Condition that the colliding GameObject has to be tagged "Weapon", (The Weapon has a nonkinematic Rigidbidy), but the Enemy simply does not react to my Player hitting it with the Weapon.
Any Ideas would be highly appreciated, Im a total Noob in Scripting(3D Artist) and would need Easy-to-follow-Instructions(and an explaination what i did wrong would be great)
Here´s my Script:
using UnityEngine;
using System.Collections;
public class EnemyNew : MonoBehaviour {
public AnimationClip IdleAnim;
public AnimationClip WakeupAnim;
public AnimationClip TargetAnim;
public AnimationClip ChaseAnim;
public AnimationClip FightAnim;
public AnimationClip AttackAnim1;
public AnimationClip AttackAnim2;
public AnimationClip WalkAnim;
public AnimationClip DieAnim;
private Animation _animation;
public Transform StartPoint;
public Transform player;
public Transform SelfRotationReferencePointLeftShoulder;
public float RotationDamping;
public float MoveSpeed;
public float WakeUpAnimSpeed = 1;
public float PlayerDistance;
public float StartDistance;
public float EnemyRotation;
void Start()
{
_animation = GetComponent();
_animation.Play (IdleAnim.name);
_animation[WakeupAnim.name].speed = WakeUpAnimSpeed;
}
void Update ()
{
PlayerDistance = Vector3.Distance (player.position, transform.position);
StartDistance = Vector3.Distance (StartPoint.position, transform.position);
if (PlayerDistance < 12f)
{
Target();
}
if (PlayerDistance < 10f)
{
if(PlayerDistance > 3f)
{
Chase();
}
}
if (PlayerDistance < 3f)
{
Fight();
}
if (PlayerDistance < 2.8f)
{
Attack();
}
if (PlayerDistance > 12f)
{
if(StartDistance < 0.1f)
{
Return();
}
}
if (StartDistance < 0.1f)
{
IsReturned();
}
}
void OnCollisionEnter(Collision Col)
{
if (Col.gameObject.tag == "Weapon")
{
_animation.Play(DieAnim.name);
}
}
void Target()
{
_animation.CrossFade (WakeupAnim.name);
_animation.CrossFade (TargetAnim.name);
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * RotationDamping);
}
void WakeUp()
{
_animation.CrossFade (WakeupAnim.name);
_animation.CrossFadeQueued (TargetAnim.name);
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * RotationDamping);
}
void Chase()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * RotationDamping);
transform.Translate (Vector3.forward * MoveSpeed * Time.deltaTime);
_animation.CrossFade (ChaseAnim.name);
}
void Fight()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * RotationDamping);
_animation.CrossFade (FightAnim.name);
}
void Attack()
{
if (SelfRotationReferencePointLeftShoulder.transform.position.x > 0f)
{
_animation.Play (AttackAnim1.name);
}
}
void Return()
{
Quaternion rotation = Quaternion.LookRotation (StartPoint.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * RotationDamping);
transform.Translate (Vector3.forward * MoveSpeed * Time.deltaTime);
_animation.CrossFade (WalkAnim.name);
}
void IsReturned()
{
transform.Translate(0, 0, 0);
transform.rotation = Quaternion.identity;
_animation.CrossFade(DieAnim.name);
_animation.CrossFade(IdleAnim.name);
}
}
↧