Hi guys,
How can I avoid two game objects (flying asteroids) clashing with each other?
I still want to be able to destroy them with shots, but not with one another.
Thank you!
Hi guys,
How can I avoid two game objects (flying asteroids) clashing with each other?
I still want to be able to destroy them with shots, but not with one another.
Thank you!
Disable collision checking between asteroids or when they collide smash them into two smaller ones. Basically whatever you have in your collision method for asteroids, remove it so they don’t get destroyed.
Without seeing some code I’m limited in how I can help you.
Here is the full script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyByContact : MonoBehaviour
{
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other) {
if (other.tag =="Boundary")
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
Could you give a hint of commands to be used to exclude the colisions or as you mentioned to smash them into 2 smaller ones?
Is that script attached to your asteroids? If it is then therein lies your problem.
Yes, that’s the one attached to the asteroids, could you give a hint what’s the issue?
You’re not checking if it’s colliding with another asteroid. You’re only checking for the boundary and the player before you issue the command to destroy it. Assuming you’re tagging them as “Asteroid” change the code starting at line 27 to the following.
if (other.tag == "Boundary" || other.tag == "Asteroid")
{
return;
}
I tried that, but didn’t work
but when I modified to the following, it worked, thanks so much for help!
if (other.tag =="Boundary")
{
return;
}
if (other.tag == "Asteroid")
{
return;
}