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 /
This question was closed Apr 25, 2016 at 04:31 PM by Internetman.
avatar image
0
Question by Internetman · Apr 25, 2016 at 11:03 AM · c#collisionrigidbody2dmovecolliding

Problem with objects colliding and IsSleeping (C#)

So I have a problem with one of my exploding enemies. The enemy first moves to a position that is set through an array, when it reaches the position the enemy will stay still and I use an if statement and ask "if the enemy == is sleeping". If it is a timer will start to countdown, when it reaches 0 the enemy explodes!

Now the problem is that there are other enemies that collide with the enemy when it is stationary, this makes the timer stutter since enemy is not "sleeping", since moves a little bit every time an object touches it!

What is the best way to solve this problem? I still want collision on the enemy even when it is stationary, but how could I make it so that the enemy is still stationary even though an object collides with it?

Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyExplodeScript : MonoBehaviour {
     
     //private Transform target;
     public Transform[] moveToPoints;
 
     //public GameObject explodingEnemy;
 
     public Transform explodingEnemyTransform;
 
 
     int movePoint = 0; 
     public float speed;
     public float explodeTimer = 3.0f;
 
     public Rigidbody2D enemyMove;
 
     
 
     void Start () 
     {
         //Find the which points the enemy should follow in the array
         moveToPoints[0] = GameObject.Find("MoveRightUp").transform;
         moveToPoints[1] = GameObject.Find("MoveLeftDown").transform;
         moveToPoints[2] = GameObject.Find("MoveRightDown").transform;
         moveToPoints[3] = GameObject.Find("MoveRightUp").transform;
         moveToPoints[4] = GameObject.Find("MoveMiddle").transform;
 
         //Set where the enemy should go with the array
         movePoint = Random.Range(0, moveToPoints.Length);
 
     }
 
 
     void Update () 
     {
         // Speed and time 
         float step = speed * Time.deltaTime;
 
         //Get the enemy moving
         transform.Translate (Vector3.MoveTowards (transform.position, moveToPoints[movePoint].position, step) - transform.position);
 
         //**Check if the enemy is stationary**
         if(enemyMove.IsSleeping())
         {
             //Start timer 
             explodeTimer -= Time.deltaTime;
         }
 
         //After 3 seconds destroy object
         if(explodeTimer <= 0.0f)
         {
             //ExplosionSpawnEnemy();
             Destroy(gameObject);
         }
 
 
 
     }

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

  • Sort: 
avatar image
0

Answer by Ankush186 · Apr 25, 2016 at 12:11 PM

Add a new Layer Enemy assign that layer to each enemy. then in Edit>Project Settings > Physics > Layer Collision Matrix turn off the Collision of Enemy with Enemy

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 Internetman · Apr 25, 2016 at 12:27 PM 0
Share

But won't that just ignore the collision? I still want the collision to work with other objects, but so that the enemy doesn't move when it is stationary

Follow this Question

Answers Answers and Comments

139 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 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 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 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 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 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 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

Distribute terrain in zones 3 Answers

Why is my Rigidbody2D not moving? (C#) 1 Answer

Objects not colliding twice 0 Answers

Issue with adding force and lerping position? Possible unity bug? 0 Answers

Multiple Cars not working 1 Answer


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