• 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 /
  • Help Room /
avatar image
0
Question by TornschuhJette · Oct 24, 2015 at 01:43 PM · scripting problemdamageenemyaidie

Adding a "Die-Event" to my Enemy-Script with OnCollisionEnter

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 $$anonymous$$gh.

Everyt$$anonymous$$ng works fine(more or less), but now i want my enemy to "die"(eg play a certain Animation) if he is $$anonymous$$t with the weapon.($$anonymous$$t Once, no facy health scripts, just a one $$anonymous$$tter).

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 $$anonymous$$tting it with the Weapon.

Any Ideas would be $$anonymous$$ghly 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>();
         _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);
     }
 }
 
 
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Damage calculation with various weapons 1 Answer

Trouble making jumping spider enemies 0 Answers

Bullet Intersect Script Problem 1 Answer

error CS0103: The name `yourCharacter' does not exist in the current context 1 Answer

Error CS0177 - "GameManager does not contain a definition for 'GetPlayer' PLEASE HELP! 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