ontrigger

hello guys ,
so I am following a tutorial and currently making an ai script for an enemy
we have the following:
1-enemy graphic with 2 2d colliders
2-a parent game object that has a box collider (with trigger checked) and a rigidbody2D

whenever the player enter into this trigger zone , the enemy starts running and attacking the player

i am having two issues :

issue 1 :
-the whole script isn’t working so I run some test
i have ontriggerenter , ontriggerstay and ontriggerexit

the ontriggerenter is not being activated , it directly goes to ontriggerstay

here is the script

using UnityEngine;
using System.Collections;

public class enemy_movement_controller : MonoBehaviour {

public float movespeed;
private Animator anim;
public GameObject enemy;
private Rigidbody2D enemyrb;

//flipping
private bool canflip = true ;
private bool facingright = false;
private float fliptime = 5;
private float nextflip = 0f;

//attacking
public float chargetime;
private float startchargetime;
private bool ischarging;

// Use this for initialization
void Start () {

	enemyrb = GetComponentInChildren<Rigidbody2D> ();
	//enemyrb = GetComponent<Rigidbody2D>();
	anim = GetComponentInChildren<Animator> ();

}

// Update is called once per frame
void Update () {

	if (Time.time > nextflip) {
		if (Random.Range (0, 10) >= 5)
		{
			flipfacing ();
		}
		nextflip = Time.time + fliptime;
	}

}

void OnTriggerEnter2D(Collider2D other)
{
	if (other.gameObject.name == "PLayer") {
		Debug.Log ("Entered");
		if (facingright && other.transform.position.x < transform.position.x) {
			flipfacing ();
		} else if (!facingright && other.transform.position.x > transform.position.x) {
			flipfacing ();
		}
		canflip = false;
		ischarging = true;
		startchargetime = Time.time + chargetime;

	}

}

void OnTriggerStay2D(Collider2D other)
{
	if (other.gameObject.name == "Player") {

		Debug.Log ("STAYED");
		if(startchargetime < Time.time)
		{
			
			
			if (!facingright) {
				enemyrb.AddForce (new Vector2 (-1, 0) * movespeed);

			} else {
				enemyrb.AddForce (new Vector2 (1, 0) * movespeed);

			}

			anim.SetBool ("ischarging", ischarging);
				
		}
	}
}

void OnTriggerExit2D(Collider2D other)
{
	if (other.gameObject.name == "Player") 
	{
		Debug.Log ("EXITED");
		canflip = true;
		ischarging = false;
		enemyrb.velocity = Vector2.zero;
		anim.SetBool ("ischarging", ischarging);
	}

}

void flipfacing()
{
	if (!canflip)
		return;
	else 
	{
		float facingx = enemy.transform.localScale.x;
		facingx *= -1f;
		enemy.transform.localScale = new Vector3 (facingx, enemy.transform.localScale.y, enemy.transform.localScale.z);
		facingright = !facingright;

	}
		
}

}

issue 2 : the player can shoot missles , whenever i deactivate the collider trigger from the parent of the enemy , i can shoot missles and it explodes on the enemy and everything is fine , when i have the collider trigger the missle just explodes at its place
here is the script for the missle

using UnityEngine;
using System.Collections;

public class RocketHit : MonoBehaviour {

public float damage;
private ProjectileController PC;
public GameObject explosioneffect;

// Use this for initialization
void Awake () {
	PC = GetComponentInParent<ProjectileController> ();
}

// Update is called once per frame
void Update () {

}

void OnTriggerEnter2D (Collider2D other)
{
	if(other.gameObject.layer == LayerMask.NameToLayer("shootable"))	
	{
		PC.removeforce ();
		Instantiate (explosioneffect,transform.position,transform.rotation);
		Destroy (gameObject);
		if (other.gameObject.tag == "enemy") 
			other.gameObject.GetComponent<EnemyHealthManager> ().hurtenemy (damage);
	
	}

}

void OnTriggerStay2D(Collider2D other)
{
	PC.removeforce ();
	Instantiate (explosioneffect,transform.position,transform.rotation);
	Destroy (gameObject);
}

}

and there is another script for the movement of the missle

using UnityEngine;
using System.Collections;

public class ProjectileController : MonoBehaviour {

private Rigidbody2D myrigid;
public float rocketspeed;

// Use this for initialization
void Awake () {
	myrigid = GetComponent<Rigidbody2D> ();
	if(transform.localRotation.z>0)
		myrigid.AddForce (new Vector2(-1,0) * rocketspeed,ForceMode2D.Impulse);
	else
		myrigid.AddForce (new Vector2(1,0) * rocketspeed,ForceMode2D.Impulse);

}
	

// Update is called once per frame
void Update () {

}

public void removeforce()
{
	myrigid.velocity = Vector2.zero;
}

}

thank you for your time and help :slight_smile:

Disclaimer: I skimmed your issue.

Regarding the blowing up, and the triggers. I know you mentioned that you have child objects as part of your ship. And whenever you disable a collider, it explodes in place, but nothing happens.

  1. Are you sure it’s hitting the gameObject that has EnemyHealthManager on it?
  2. Do the child objects have the same tag as the parent?

From the looks of your OnTriggerEnter, you have it so if it hits an object…it explodes. Great.
Is it hitting your ship? One simple solution is to add:

Debug.Log(“I am hitting:” + other.gameObject.name); to the OnTriggerEnter.

You can also add a debug.log into the OnTriggerStay, as you’re also exploding it there too. Why is this there? as a backup? It shouldn’t be needed, as missiles would ideally blow up when they hit something, not when they hang out with them.