- Home /
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
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.
Indeed, keep your eye on the Hierarchy to see if these objects are disappearing or simply relocating.
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.
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
![](https://koobas.hobune.stream/wayback/20220612041240im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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