OnTriggerEnter2D only triggers once, until the character or enemy moves again. (With video showcase)
So i have this weird problem, where i can only hit an enemy once until either the enemy or the character moves again. Here is my hurtEnemy script, attached to the "swing animation" of the sword:
using UnityEngine;
using System.Collections;
public class HurtEnemy : MonoBehaviour {
public int baseDamage;
private int damageToGive;
public GameObject damageBurst;
public Transform hitPoint;
public GameObject damageNumber;
private PlayerStats thePS;
// Use this for initialization
void Start () {
thePS = FindObjectOfType<PlayerStats>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Enemy")
{
damageToGive = baseDamage + thePS.currentAttack;
other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToGive);
Instantiate(damageBurst, hitPoint.position, hitPoint.rotation);
var clone = (GameObject)Instantiate(damageNumber, hitPoint.position, Quaternion.Euler(Vector3.zero));
clone.GetComponent<FloatingNumbers>().damageNumber = damageToGive;
}
Debug.Log("Trigger activated");
}
}
If i disable my enemyMovement script, and walk up to an enemy i can hit it once, and it works perfect. But if i try to hit it again, the animation fires, but the OnTriggerEnter2D doesn't detect anything. If i then take 1 step, and it doesn't matter if it is closer, furthere away or whatever, then i am able to hit the enemy again. The same thing happens if the enemy moves. How can this be? The swing animation, which the hurtEnemy script and the triggercollider is on, is disabled and only enabled when the player is attacking.
At the bottom of this post, i have also posted a youtube video where i showcase the problem in 10 seconds. If it is to any use, here is my playerController script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private float currentMoveSpeed;
public float diagonalMoveModifier;
private Animator anim;
private Rigidbody2D myRigidbody;
private bool playerMoving;
private Vector2 lastMove;
private static bool PlayerExists;
private bool attacking;
public float attackTime;
private float attackTimeCounter;
public string startPoint;
public bool canMove;
private GameManager GM;
// Use this for initialization
void Start () {
GM = FindObjectOfType<GameManager>();
canMove = true;
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
if (!PlayerExists)
{
PlayerExists = true;
DontDestroyOnLoad(transform.gameObject);
}
else
{
Destroy(gameObject);
}
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Escape))
{
if(GM.STATE == GameManager.GameState.Playing)
{
GM.changeState(GameManager.GameState.PauseMenu);
}
else if(GM.STATE == GameManager.GameState.PauseMenu)
{
GM.changeState(GameManager.GameState.Playing);
}
}
playerMoving = false;
if (canMove)
{
if (!attacking)
{
//movement
//horizontal
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * currentMoveSpeed, myRigidbody.velocity.y);
playerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0);
}
//vertical
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * currentMoveSpeed);
playerMoving = true;
lastMove = new Vector2(0, Input.GetAxisRaw("Vertical"));
}
if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
{
myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
}
if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
}
if (Input.GetKeyDown(KeyCode.Space))
{
attackTimeCounter = attackTime;
attacking = true;
myRigidbody.velocity = Vector2.zero;
anim.SetBool("Attack", true);
}
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f && Input.GetAxisRaw("Vertical") > 0.5f)
{
currentMoveSpeed = moveSpeed * diagonalMoveModifier;
}
else
{
currentMoveSpeed = moveSpeed;
}
}
if (attackTimeCounter > 0)
{
attackTimeCounter -= Time.deltaTime;
}
if (attackTimeCounter <= 0)
{
attacking = false;
anim.SetBool("Attack", false);
}
}
else
{
myRigidbody.velocity = Vector2.zero;
}
anim.SetBool("PlayerMoving", playerMoving);
anim.SetFloat("LastMoveX", lastMove.x);
anim.SetFloat("LastMoveY", lastMove.y);
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
}
}
I have been trying to figure this out, but i just don't know why it shouldn't work. Is it a unity bug? Or do i have som logical error somewhere?
Here is a video of me showcasing the problem (only 10 seconds long): RPG attack problem video
im using Unity 5.4.1f1
Thanks for your time!
EDIT: I've tried fooling around with using OnTriggerStay instead and using some sort of counter, and it has the exact same result! It only fires the OnTriggerStay, if i attack max 1 sec after my last movement.
EDIT2; updating to unity 5.5.0f3 didn't help. This is driving me nuts
I discovered, that the trigger only works, when swing big is activated while the character is moving or has been moving recently (like in the past second). The swingBig OnTriggerEnter will never activate, while standing still for more than a sec.
Answer by Lund259 · Dec 01, 2016 at 02:30 PM
I just solved the issue. Just writing it here if someone else are experiencing the same issue. All i did was i added a rigidbody2D to my swingBig animation, and checked IsKinematic. Thats is. Can't believe this issue bugged me for days lol!