Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by nasby321 · Nov 09, 2013 at 08:43 PM · c#enemydeath

How to make my enemy stop moving once dead?

I tried several ways, but it seems no matter what I try it doesn't work. He either sits still before he dies or the game object goes crazy and flys off screen.

 using UnityEngine;
 using System.Collections;
 using SpriteFactory;
 
 
  
 public class EnemyAi : MonoBehaviour {
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     public int maxDistance = 8;
     private Sprite sprite;
     public AudioClip swords;
     public int life = 100;
     public int points = 15;
     
     
  
     private Transform myTransform;
     
      IEnumerator Death() {
    yield return new WaitForSeconds(0.8f);
         
         Destroy(gameObject);
         PointCounter.points += points;
         
         
     }
  
     void Awake() {
        myTransform = transform;
        sprite = (Sprite)GetComponent(typeof(Sprite));
     }  
  
     
     void Start () {
        GameObject go = GameObject.FindGameObjectWithTag("Player");
  
        target = go.transform;
  
        maxDistance = 8;
 
     }   
  
     void FixedUpdate () {
        if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
  
          myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime;{
              sprite.Play ("EnemyWalk");
                  if (life <=0) {
                     sprite.Stop ();
                 }
 }
 }
 }
     
     //Health and damage rate when enemy is hit
         void OnTriggerEnterSprite(SpriteCollider.CollisionData data) {
             sprite.Play("hurt");
                 if(sprite.IsAnimationPlaying ("hurt")){
                     audio.Play();{
                          life += -100;
                             if (life <= 0) {
                              
                                 sprite.Play ("death");
                                     StartCoroutine(Death());
                                     }
         
 }
 }
 }
 }
 
Comment
Add comment · Show 3
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
avatar image Huacanacha · Nov 10, 2013 at 09:02 AM 0
Share

To clarify, do you want the enemy to remain in the same position for 0.8 seconds after being killed, then to disappear?

Assu$$anonymous$$g that's the case, and taking a look at the code I will say this... If you don't want something to move after it has zero life, then don't move it after it has zero life. Try changing the if clause in FixedUpdate() to:

 if(life > 0 && Vector3.Distance(target.position, myTransform.position) > maxDistance) {
avatar image nasby321 · Nov 10, 2013 at 01:42 PM 0
Share

What do I put after it? Should I set a new speed and max distance for it? The sprite.Stop(); part is to stop the walking animation and for it to switch to the death animation.

avatar image Huacanacha · Nov 10, 2013 at 11:45 PM 0
Share

Did you try it as is? Obviously you would remove the "if (life

You play the death animation in OnTriggerEnterSprite. A 'stop' animation in addition to a 'death' animation seems redundant, do you really need both? If you need to play 'stop' then 'death' then chain the animations one after the other.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by spiralfire11 · Nov 09, 2013 at 10:31 PM

Maybe using OnCollision would work since that's what I use; Unfortunetaly, that would mean if you would get touched by the enemy, you would die.

Comment
Add comment · Show 1 · Share
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
avatar image nasby321 · Nov 10, 2013 at 02:16 AM 0
Share

I want the Enemy itself to stop moving once I kill it. After I defeat it, it will move towards me if I move away for the amount of time the Coroutine lasts.

avatar image
0

Answer by Lyndon88 · Oct 15, 2014 at 01:54 PM

if life = 0 movement =false

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Simple enemy kill count 1 Answer

Audio Issue, Plays with PlayOneShot but not Play 0 Answers

Enemy Spawn Script Spawning Infinite Enemies [Help] 2 Answers

Distribute terrain in zones 3 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