Boxcollider not dealing damage unless moving | Unity2D
I'm trying to set up an "attack" using boxcolliders, I've set up so that the collider attached to the player character becomes active when you press a button and is supposed to take health from a object tagged as enemy, but the enemy only takes damage when its moving into the active collider or vice versa, but not when stationary.
these are my scripts:
public class PlayerAttack : MonoBehaviour {
private bool attacking = false;
private float attackTimer;
private float attackCd = 0.3f;
public Collider2D attackTrigger;
void Start () {
//turns off hitbox collider at start
attackTrigger.enabled = false;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("k") && !attacking)
{
attacking = true;
attackTimer = attackCd;
//turns on hitbox collider if
attackTrigger.enabled = true;
}
if (attacking)
{
if(attackTimer > 0)
{
//subtracts to countdown the cooldown timer
attackTimer -= Time.deltaTime;
}
else
{
//turns off hitbox collider after attack
attacking = false;
attackTrigger.enabled = false;
}
}
}
}
public class AttackTrigger : MonoBehaviour {
public int dmg = 20;
private void OnTriggerEnter2D(Collider2D col)
{
if (col.isTrigger != true && col.CompareTag("Enemy"))
{
col.SendMessageUpwards("Damage", dmg);
}
}
}
public class EnemyBehavior : MonoBehaviour {
public float currHealth;
// Update is called once per frame
void Update () {
if(currHealth <= 0)
{
Destroy(gameObject);
}
}
public void Damage(int damage)
{
currHealth -= damage;
}
}
EDIT: Here is the movement script
public class PlayerMovement : MonoBehaviour {
public float movementSpeed;
public float jump;
public Rigidbody2D rb;
public bool onGround = true;
public float dblJmp = 0f;
public float dashSpeed;
public float dashTime;
public float dashInc;
public float dashMax;
// Use this for initialization
void Start () {
}
void Update() {
//moves player right
if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vector3.right * Time.deltaTime * movementSpeed);
if (Input.GetKeyDown("j"))
{
rb.AddForce(transform.right * dashSpeed);
rb.velocity = Vector3.zero;
}
}
//moves player left
if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(Vector3.left * Time.deltaTime * movementSpeed);
if (Input.GetKeyDown("j"))
{
rb.AddForce((-1 * transform.right) * dashSpeed);
rb.velocity = Vector3.zero;
}
}
//makes player jump and double jump and checks if player is on ground before allowing jump
if ((Input.GetKeyDown("w") || Input.GetKeyDown(KeyCode.UpArrow)) && (onGround == true) /*&& (dblJmp < 2)*/)
{
//sets velocity of jumping player to zero before jump to make jump height consistent
rb.velocity = Vector3.zero;
rb.AddForce(transform.up * jump);
dblJmp++;
}
if (dblJmp < 2)
{
onGround = true;
}
else
{
onGround = false;
}
}
//checks if player is on the ground, and sets double jump counter to zero if on ground
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
dblJmp = 0f;
onGround = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
onGround = false;
}
}
}
I've used debug.log and it seems the problem has to do with the if statement in the AttackTrigger script, but I'm not sure what.
Answer by binaryuniverse · Jun 25, 2018 at 11:01 PM
If you circumvent normal physics operations, you'll need to manually check interactions.
Use the Physics.Overlap functions (for example, Physics,OverlapBox, Physics.OverlapSphere)
I tried doing this but I'm running into the same problem, where nothing will happen unless the collider is moving.
I've used debug.log and it seems the problem has to do with the if statement in the AttackTrigger script, but I'm not sure what.
I just noticed that you're calling the trigger function but expect the other object to NOT be a trigger (i.e. a regular collider)
If you don't have a rigidbody attached to at least ONE of these colliders, the collision will never register. Take a look at the collision matrix at the bottom of this screen, it may help you plan your next strategy.
https://docs.unity3d.com/$$anonymous$$anual/CollidersOverview.html
Actually, I just noticed that you said the interactions DO occur when they move. Let me keep looking at your code. How do you move your objects? Do you use transform.position translate or do you use physics.addforce or change velocity?
O$$anonymous$$, I think I might see the problem. You're looking for both objects to be stationary and for the attack to register, but you selectively turn on and off the collider. OnTriggerEnter functions only occur during entry. You might be missing that (time-wise.) You could try using OnTriggerStay ins$$anonymous$$d. The only detail is if you want discrete hits, you'll have to add a time counter to prevent hit spam$$anonymous$$g. So, register a hit immediately, then wait for e.g. 0.5 seconds before registering the next one (the time will depend on the constraints of your game.)
So I've tried OnTriggerStay2D but it still does the same thing, the if statement doesn't go through.
I've added the movement script to the original question if that helps.
Your answer
Follow this Question
Related Questions
Overlapping colliders with Raycasting in 2D 0 Answers
Calculate BoxCollider2D based on the actual player sprite 2 Answers
Checking OnCollisionEnter2D names with TilemapCollider2D 0 Answers
2DCollider Issues 1 Answer
My ground checker only works sometimes 0 Answers