OnTriggerEnter gets called only once
Hey guys
I try to develop a simple 2d game by unity. It's about a little knight, who uses a sword as a melee attack. So I tried to use the function OnTriggerEnter whenever an enemy is in range and you press space. To test it, I wrote a little text in the Debug.Log() Method. Unfortunatly, it gets called only once regardless how often I press space. Although, it works fine while I run into the enemy or choose the other side to attack it.
Do you have any ideas what the problem is?
I copied both scripts down below, just in fact that you want to have a look at them
Thanks for any help in advance. I'm really desperated and annoyed to be honest
And sorry for my English.
Attack script:
public BoxCollider2D range;
private bool attacking = false;
bool pressedSpace = false;
private Animator anim;
void Start()
{
range = gameObject.GetComponent<BoxCollider2D>();
anim = GetComponent<Animator>();
range.enabled = false;
}
void Update()
{
pressedSpace = false;
anim.SetBool("pressedSpace", pressedSpace);
attacking = false;
range.enabled = false;
if(Input.GetKeyDown(KeyCode.Space) && !attacking)
{
attacking = true;
range.enabled = true;
pressedSpace = true;
anim.SetBool("pressedSpace", pressedSpace);
}
}
attack Trigger:
public int dmg = 30;
void OnTriggerEnter2D(Collider2D col)
{
if(col.isTrigger != true && col.CompareTag("Enemy"))
// col.SendMessageUpwards("Damage",dmg);
Debug.Log("works");
}
Answer by SpaceManDan · Oct 08, 2016 at 03:07 PM
Im pretty sure you just need to change your trigger to a stay trigger.
private void OnTriggerStay2D(Collider2D col)
{
if(col == player)
{
playerIsInRange = true;
}
}
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerStay2D.html
Also you might need to use
private void OnTriggerExit2D(Collider2D col)
{
if(col == player)
{
playerIsInRange = false;
}
}
to know when the player is no longer in range. https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerExit2D.html
EDIT:
Ok, you need to step back a bit and redesign the way you are doing this I think. If it was me I would do the following.
In the Unity Scene you should have:
1 Player gameobject. The Tag on the Player gameObject should be set to "Player". This gameobject should also have a collider enabled (NOT A TRIGGER COLLIDER).
1 Player Trigger Radius gameobject. This is a child of Player gameObject. Player Trigger Radius should have a collider set to is trigger. The Mesh Renderer should be set to inactive (turned off) on this gameobject so it is not visible at run time. Only the collider should be active acting as a proximity field for the player. IMPORTANT: Set the size of this gameobject to represent the radius of the players attack range. If an enemy collider passes into this trigger collider it will send a call to OnTriggerEnter() and OnTriggerStay. When the enemy collider exits this trigger collider a call to OnTriggerExit() is made.
1 Enemy gameobject with a Tag set to "Enemy" or whatever you like. Also, the Enemy GameObject needs a collider enabled (NOT A TRIGGER COLLIDER).
1 C#Script named Player place it onto the Player gameobject.
private PlayerTriggerRadius playerTriggerRadius;
private bool tryAttack;
private void Start()
{
playerTriggerRadius = FindObjectOfType<PlayerTriggerRadius>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space)
{
tryAttack = true;
}
if (playerTriggerRadius.enemyInRange == true && tryAttack == true)
{
//PUT CODE FOR ENEMY IN RANGE ATTACK HERE. THIS CASE WILL OCCUR WHEN SPACE IS PRESSED AND THE ENEMY IS IN RANGE.
Debug.Log("Player just attacked and hit the enemy.");
tryAttack = false;
}
else if (playerTriggerRadius.enemyInRange == false && tryAttack == true)
{
tryAttack = false;
}
}
1 C#Script named PlayerTriggerRadius. Make sure you dont forget to put it onto the Player TriggerRadius gameObject.
public bool enemyInRange;
private void OnTriggerEnter(Collider col)
{
if (col.tag == "Enemy")
{
enemyInRange = true;
}
}
private void OnTriggerExit(Collider col)
{
if (col.tag == "Enemy")
{
enemyInRange = false;
}
}
NOW... If you do everything here just like this. The player will hit the enemy when they are overlapping the player trigger collider game object. Ask if you get confused about anything and I'll try to clarify. Hope this helps.
First, thanks for your fast question. I tried OnTriggerStay2D out. The result is that it gets called 27 times, regardless wether I press space or not... what I found again quite annoying. I searched for the mistake and I found a quite weird thing in my opinion.
This trigger, which gets triggered once an enemy is inside of it, is parented by my player object. To control the boxcollider2D I attached it to the attack script of my player. So it should gets enabled and disabled depending on wether I press space or not but the only thing which gets disabled is one of the boxcollider2D of my player???
So I attached a totally different boxcollider2D and the only result is that one of my player boxcollider2d gets affected???!!!
I'm now totally confused and I don't have the smallest idea where the mistake is or wether I possibly run into a bug...
@$$anonymous$$agJ99 I edited my post. Lost for you to do up there but it should work when you are done. Let me know if you get stuck.
Your answer
Follow this Question
Related Questions
No overload for method. (Question) 0 Answers
Jump off 2D platforms Smash brothers style. 1 Answer
OnTriggerStay2D() for 2 colliders simultaneously 1 Answer
OnCollisionEnter2D is not being called 0 Answers