Hello, I have been working a long time on this problem but i can’t figure out how to fix it.
Here is the idea:
I am making a FPS game, you shot arrow, throw rock at alien to kill them. (being ragdoll for 2 second than destroy gameObject).
The thing is you can also throw bomb at alien to make them explode.
I want the alien to detect the explosion collision and activate the dead function too.
The problem is: the alien does get push back when the bomb explode but it can’t detect if they get hit by the explosion so that they can being dead.
It only push them backward but they are still attacking the player, they are not dead because they haven’t detected if they were hit by an explosion of a bomb.
When the player throw a bomb, it activate the Explosion.cs script (detonate()):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explosion : MonoBehaviour
{
public float power = 10.0F;
public float radius = 5.0F;
public float upForce = 1.0F;
public IEnumerator Explode() {
ParticleSystem exp = GetComponent<ParticleSystem>();
exp.Play();
Destroy(gameObject, exp.main.duration);
yield return new WaitForSeconds(2);
gameObject.GetComponent<Renderer>().enabled = false;
gameObject.GetComponent<Rigidbody>().freezeRotation = true;
detonate();
gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
gameObject.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}
private void detonate()
{
Vector3 explosionPostion = gameObject.transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPostion, radius);
foreach (Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddExplosionForce(power,explosionPostion,radius,upForce,ForceMode.Impulse);
}
}
}
}
The alien got an script called EnnemieAI.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnnemieAI : MonoBehaviour
{
public Transform player;
public healthPlayer playerHealth;
public float distanceTriggerAggresive = 100;
public float moveSpeed = 10f;
private Rigidbody rb;
private bool isDead = false;
private bool attacking = false;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance (player.position, this.gameObject.transform.position);
if (!isDead)
{
if (distance <= distanceTriggerAggresive)
{
Vector3 playerPosWithoutY = new Vector3(player.position.x,0,player.position.z);
Vector3 pos = Vector3.MoveTowards(transform.position, playerPosWithoutY, moveSpeed * Time.fixedDeltaTime);
rb.MovePosition(pos);
transform.LookAt(player);
}
else if (distance > distanceTriggerAggresive)
{
gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
gameObject.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}
}
else
{
Destroy(this.gameObject, 2);
}
if (attacking)
{
attackPlayer();
}
}
private void attackPlayer()
{
playerHealth.getHit(0.5f);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "barreArbalete(Clone)" || collision.gameObject.name == "Comête")
{
isDead = true;
Destroy(this.gameObject, 2);
}
}
private void OnCollisionExit(Collision collisionInfo)
{
attacking = false;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name != "Terrain")
Debug.Log(other.name);
if (other.gameObject.name == "Joueur")
{
attacking = true;
}
}
}
Here the EnnemieAi script can detect if the alien gameObject got a collision by a arrow “barreArbalete(Clone)” shot by the player or by a rock “Comête” throw by the player then activate the dead function.
By i can’t make the script detect for the collision by the Rigidbody of Collider[ ] colliders in the explosion script.
I can’t make the EnnemieAI script detect the Collider[ ] colliders of the Explosion script by the
[OnCollisionEnter] or [OnTriggerEnter] function even if it push back all the object around.
However it can detect the bomb gameobject but not his explosion
I have made this work by making the explosion script called a dead function of alien when one of the rigidbody get hit by a collision but it make the code less good, less logical and i work only for an instance of alien in the scene.
Thanks in advance for all of your response. ![]()