Hello. I’m fairly new and have been trying to get an enemy to attack my player when the player is in range of a sphere collider. Unfortunately, collisions for OnTriggerEnter and OnTriggerExit are not registering.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour {
public float period = 0.5f;
public int attackDamage = 10;
Animator anim;
GameObject player;
HealthControl playerHealth;
EnemyHealth enemyHealth;
bool playerInRange;
float timer;
void Awake ()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<HealthControl>();
enemyHealth = GetComponent<EnemyHealth>();
//animator
}
void onTriggerEnter(Collider other)
{
if(other.gameObject == player)
{
Debug.Log("InRange");
playerInRange = true;
}
}
void onTriggerExit(Collider other)
{
if(other.gameObject == player)
{
playerInRange = false;
Debug.Log("NotInRange");
}
}
void Update ()
{
timer += Time.deltaTime;
if(timer >= period && playerInRange && enemyHealth.currentHealth > 0)
{
Attack();
}
}
void Attack()
{
// Reset the timer.
timer = 0f;
Debug.Log("EnemyAttack");
// If the player has health to lose...
if (playerHealth.currentHealth > 0)
{
// ... damage the player.
playerHealth.TakeDamage(attackDamage);
}
}
}
Above is the code for my attack script. I’m using a modified version of the Unity tutorial one. However, not even the Debug.Logs are showing up, meaning that a collision must not be registering.
Here are the two objects that should interact. Both have rigidbodies and colliders. The enemy has a IsTrigger spherical collider along with its capsule collider. However, there’s no collisions.
I’ve researched for a while, and it seems most problems are due to a lack of rigidbodies or IsTrigger not being checked, or colliders, but these are already covered. However, it is still not working. I’m not sure if I’m missing some small bit of information, or missed a tiny detail I had to check.
@Jaimi I have two spheres with RGBs, colliders, on same layer, each with OnCollisionEnter in the single script on both GOs.
I created a third object duplicated from one of the spheres.
I run the game and move the 3rd GO to create an impact yet there is no debug.log report. The RGBs do bounce off each other.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class HandedCollision : MonoBehaviour
{
public GameObject m_leftCollider;
public GameObject m_rightCollider;
public GameObject m_TmLeft;
public GameObject m_TmRight;
private TextMeshProUGUI lefttextmesh;
private TextMeshProUGUI righttextmesh;
private int leftcollisionCount = 0;
private int rightcollisionCount = 0;
void OnCollsionEnter(Collision col)
{
Debug.Log("incoming collision: "+ col.gameObject.name);
switch (col.gameObject.name)
{
case "Lefty":
++leftcollisionCount;
lefttextmesh.text =
"<b><u><#ffff00>" + col.gameObject.name +
":</color></u></b>" + "\n" +
"Collision: " +
leftcollisionCount + "\n";
break;
case "Righty":
++rightcollisionCount;
righttextmesh.text =
"<b><u><#ffff00>" + col.gameObject.name +
":</color></u></b>" + "\n" +
"Collision: " +
rightcollisionCount + "\n";
break;
default :
++leftcollisionCount;
lefttextmesh.text = ":" + col.gameObject.name + ": " + leftcollisionCount;
++rightcollisionCount;
righttextmesh.text = ":" + col.gameObject.name + ": " + ++rightcollisionCount;
break;
}
}
// Start is called before the first frame update
void Start()
{
lefttextmesh = m_TmLeft.GetComponent<TextMeshProUGUI>();
lefttextmesh.text = "<b><u><#ffff00>LeftHandedCollider</color></u></b>";
righttextmesh = m_TmRight.GetComponent<TextMeshProUGUI>();
righttextmesh.text = "<b><u><#ffff00>RightHandedCollider</color></u></b>";
}
}
This should be simple and is straight out of the demos I have seen. I kept it simple for just this reason
Thanks in advance.
The collision was detected in the editor, but NOT in in builds!
The PROBLEM are the TAGS!
If you go to Project Settings → Tags and Layers → under Tags are (Removed!) items.
All items after the (Removed!) item don’t work in Builds.
The SOLUTION was to close Unity and open the project again.
It seems the player can’t handle this. The Editor can.
And the real problem was not the collision, it was the Tag. (Unity 2019.2.0f1)
I wanted to add for anyone else having this issue, make sure you’re using the right version of OnTriggerEnter(). There is a 2D version : void OnTriggerEnter2D(Collider2D other)
And to have a brief look at the available forums and try to post in the correct one where appropriate as you’ll get better feedback. The physics forum is here.