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 Internetman · Apr 26, 2016 at 01:29 PM · c#collisionmovementcollidercolliding

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 will explode!

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

· Add your reply
  • Sort: 
avatar image
0

Answer by gcoope · Apr 26, 2016 at 01:49 PM

Use a bool? The first time it becomes stationary store it in a variable - something like hasBecomeStill - then check that it isSleeping as well as hasBecomeStill is equal to true.

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 26, 2016 at 02:06 PM 0
Share

That would kind of work, but if an object constantly collides with the enemy when it is supposed to be "stationary", it will not set the bool to true, any idéas my friend?

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

142 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 avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Stopping my player from moving when hitting a wall. 5 Answers

Objects not colliding twice 0 Answers

Unity Colliders are Overlapping/Intersecting 2 Answers

How to hold objects in third person? 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