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