Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 PokeRwOw · Oct 14, 2015 at 09:53 AM · collidertransformcoroutine

Enemies disappear randomly

I have implemented various enemies in my game but for now, I'm confronted with a problem when I want to add pre-defined movement to all enemies.

Firstly, the code managing enemies worked fine. The enemies just waited and if the players was too close to him, he attacked.

 [SerializeField]
 private float rotationSpeed = 180;

 [SerializeField]
 private float movementSpeed = 1f;

 [SerializeField]
 private float meshRadius = 1f;

 private IEnumerator turnTowardsPlayerCoroutine;
 private IEnumerator moveTowardsPlayerCoroutine;

 private bool isDead = false;

 public float speed = 1;

 private int maxLife = 3;

 void OnTriggerEnter(Collider collider)
 {
     if (collider.gameObject.tag == "Player")
     {
         float playerDistance = Vector3.Distance(collider.transform.position, transform.position);

         if (playerDistance >= 2f * meshRadius)
         {
             turnTowardsPlayerCoroutine = TurnTowardsPlayer(collider.transform);
             moveTowardsPlayerCoroutine = MoveTowardsPlayer(collider.transform);
             StartCoroutine(turnTowardsPlayerCoroutine);
             StartCoroutine(moveTowardsPlayerCoroutine);
         }
     }
 }

 void OnTriggerExit(Collider collider)
 {
     if (collider.tag == "Player")
     {
         float playerDistance = Vector3.Distance(collider.transform.position, transform.position);

         if (playerDistance >= 2f * meshRadius)
         {
             StopCoroutine(turnTowardsPlayerCoroutine);
             StopCoroutine(moveTowardsPlayerCoroutine);
         }
     }
 }

 void OnDeath()
 {
     if (isDead)
     {
         return;
     }

     maxLife--;

     if (maxLife <= 0)
     {
         isDead = true;

         GameManager.Instance.NumKilledEnemies++;

         StopCoroutine(turnTowardsPlayerCoroutine);
         StopCoroutine(moveTowardsPlayerCoroutine);

         Destroy(gameObject);
     }
 }

 private IEnumerator TurnTowardsPlayer(Transform player)
 {
     while (true)
     {
         Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position, Vector3.up);
         targetRotation.x = 0f;
         targetRotation.z = 0f;

         transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
         yield return 0;
     }
 }

 private IEnumerator MoveTowardsPlayer(Transform player)
 {
     while (true)
     {
         Vector3 playerDirection = transform.position - player.position;
         playerDirection.y = 0;
         playerDirection = playerDirection.normalized;

         Vector3 deltaMovement = playerDirection * movementSpeed * Time.deltaTime;

         int layermask = LayerMask.GetMask("Environment");
         Vector3 movingTowards = transform.position - playerDirection * meshRadius + (new Vector3(0f, 0.1f, 0f));
         if (Physics.Raycast(movingTowards, Vector3.down, 0.25f, layermask))
         {
             transform.position -= deltaMovement;
         }

         yield return 0;
     }
 }

But after that, I wanted to add a predefined movement of enemies if nobody is close to him so I did something like this :

 private IEnumerator moveDefaultCoroutine;

 void Start()
 {
     moveDefaultCoroutine = MoveToPosition(new Vector3(transform.position.x, transform.position.y, transform.position.z + 3), 5);
     StartCoroutine(moveDefaultCoroutine);
 }

 private IEnumerator MoveToPosition(Vector3 newPosition, float time)
 {
     while (true)
     {
         float elapsedTime = 0;
         Vector3 startingPos = transform.position;

         while (elapsedTime < time)
         {
             transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
             elapsedTime += Time.deltaTime;
             yield return null;
         }

         yield return new WaitForSeconds(2);
         elapsedTime = 0;

         while (elapsedTime < 0.8)
         {
             transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, new Vector3(0, 180, 0), (elapsedTime / 0.8f));
             elapsedTime += Time.deltaTime;
             yield return null;
         }

         elapsedTime = 0;

         while (elapsedTime < time)
         {
             transform.position = Vector3.Lerp(newPosition, startingPos, (elapsedTime / time));
             elapsedTime += Time.deltaTime;
             yield return null;
         }

         yield return new WaitForSeconds(2);
         elapsedTime = 0;

         while (elapsedTime < 0.8)
         {
             transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, new Vector3(0, 0, 0), (elapsedTime / 0.8f));
             elapsedTime += Time.deltaTime;
             yield return null;
         }
     }
 }

And here come the problem, when I'm adding this code the enemies disappear randomly... Did I did something wrong ?

Full code here : http://pastebin.com/tzGczv26

Thanks per advance !

PokeRwOw

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dudester · Oct 14, 2015 at 12:03 PM

not sure if this is 2d or 3d but could you not use character controllers or rigidbodys for movement as appose to just moving the positions of the object ? cause by my reasoning this seems like maybe the objects are falling through the collision object and falling out of the world , perhaps not but it seems likley , maybe replace the vector3.lerp with transform.translate ? just some ideas to try . let me know if you need more help.

Comment
Add comment · Show 3 · 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 meat5000 ♦ · Oct 14, 2015 at 12:44 PM 0
Share

Indeed, keep your eye on the Hierarchy to see if these objects are disappearing or simply relocating.

avatar image PokeRwOw meat5000 ♦ · Oct 14, 2015 at 05:49 PM 0
Share

The objects disappearing on screen, but in the Hierarchy they are relocating in the sky and falls to the ground... And this problem seem to be appear if they are 2 types of the same enemy in the same level.

avatar image dudester · Oct 14, 2015 at 10:49 PM 0
Share

if these objects have character controllers and they are in the same location one will pop up into the sky while the other might fly off in a random direction , this happens very fast , seems to be glitch with unity .

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

After building move doesnt move 1 Answer

Translate.transform problems (collision/Rigibody) 0 Answers

Weird shaky player behavior (moving through colliders) 0 Answers

Finding a RaycastHit tag (Null Reference Exception) 2 Answers

Cars driving the wrong way 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