- Home /
Need help ignoring collision
Earlier today, I asked for help regarding how to avoid collision between two instantiated gameobjects.
With my limited understanding on programming, perhaps a question on the general use of it was a bit over my head.
Instead, please help me by looking at my code, poor as it may be, and help me with my problem.
I need to make Missile and Enemy ignore collision with each other.
using UnityEngine;
using System.Collections;
public class EnemyBehaviour : MonoBehaviour {
public GameObject Missile;
public GameObject Player;
public float enemySpeed;
private int health;
private float time;
private int shotFired;
private float shotTimer;
void OnCollisionEnter(Collision target)
{
if(target.transform.name == "Bullet(Clone)")
{
health--;
}
}
// Use this for initialization
void Start () {
health = 3;
time = Time.deltaTime;
shotFired = 0;
}
// Update is called once per frame
void Update () {
if(PlayerBehaviour.playerHeight - transform.position.y >=0.3)
{
transform.Translate(-15f * time,10f * time,0);
}
else if(PlayerBehaviour.playerHeight - transform.position.y <=-0.3)
{
transform.Translate(-15f * time,-10f * time,0);
}
else
{
transform.Translate(-15f * time,0,0);
}
shotTimer += 1 * Time.deltaTime;
if(shotTimer >= 0.4 && shotFired == 0)
{
shoot();
}
if(transform.position.x < -40)
{
Destroy(gameObject);
}
if(health<=0)
{
Destroy (gameObject);
}
}
void shoot()
{
if(shotFired == 0)
{
Instantiate(Missile, new Vector3(transform.position.x+3,transform.position.y,transform.position.z),Quaternion.identity);
shotFired = 1;
}
}
}
That is for the Enemy
using UnityEngine;
using System.Collections;
public class MissileBehaviour : MonoBehaviour {
private float heightSpeed;
private int directionSet;
private float fireTime;
void OnCollisionEnter(Collision other)
{
if(other.transform.name == "Ship")
{
Application.LoadLevel("Menu");
}
}
// Use this for initialization
void Start () {
directionSet = 0;
}
// Update is called once per frame
void Update () {
fireTime += 1 * Time.deltaTime;
if(PlayerBehaviour.playerHeight != 0)
{
if(directionSet == 0)
{
heightSpeed = -((PlayerBehaviour.playerHeight - transform.position.y)/(PlayerBehaviour.playerX - transform.position.x-2));
directionSet = 1;
}
if(fireTime >= 1.0)
{
transform.Translate (-100f*Time.deltaTime,heightSpeed*100*Time.deltaTime,0);
}
}
}
}
That is for the missile
Answer by rutter · Jun 15, 2012 at 04:57 AM
Unity's physics engine allows you to set a layer for each GameObject. It's also possible to set flags to control which layers will collide with each other. You can read more about this in the Unity manual. If you're spawning with prefabs, you can set the prefab's layer using the inspector. If it's easier in your case, you could also set a bullet's layer right after spawning it, from script.
If you only need to ignore collision between two specific colliders -- say, a bullet and the character who shot it -- you can call `Physics.IgnoreCollision()` to ignore collisions between any two colliders. For this method, you'll have to do it in script after creating the bullet.
Maybe this is still more general than you're looking for. Is there some specific part of this process that's giving you trouble?
Thank you for answering.
Yes, this is still a bit too general for me.
I understand that Physics.IgnoreCollision() will do what I need, but the problem is that I don't know how to use it.
This might just be me who is a total noob at Unity, but I don't know how to find the collider of the missile I'm shooting.
For instance,
Physics.IgnoreCollision(missile.collider,enemy.collider);
From what I understand, I must first find the GameObject of the missile and the enemy, but since both of them are prefabs, I don't know how to do so.
Up until now, whenever I've had to use another GameObject I would manually find it in the script and then find the specific GameObject in my scene.
This is not possible to do with instantiated GameObjects however, so how do I find these?
TL;DR:
How do I find the GameObjects of something instantiated and use said GameObjects collider in Physics.IgnoreCollision()
that is a question for another thread uyour question has been answered for you and should be marked as answered, on the side note to save you time filling out another question you need to set a var to the game object getComponet collider then use it in the Physics.IgnoreCollision function.