- Home /
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);
}
}
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.
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
Follow this Question
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