- Home /
Question by
Valorous_Entity · Mar 18, 2016 at 12:29 PM ·
polygon collider 2dhitbox
Polygon Collider 2D Problem.
Okay so basically my main character uses a sword to attack and I have made a polygon collider around it to use for collisions against enemies. Everything is set up, but when I attack, multiple enemies take damage at the same time instead of just one. It's very confusing and I haven't been able to solve this problem. Here is the polygon collider script
using UnityEngine;
using System.Collections;
public class HeroHitBoxManager : MonoBehaviour {
// Set these in the editor
public PolygonCollider2D frame2;
public PolygonCollider2D frame3;
public PolygonCollider2D frame4;
// Used for organization
private PolygonCollider2D[] colliders;
// Collider on this game object
private PolygonCollider2D localCollider;
// We say box, but we're still using polygons.
public enum hitBoxes
{
frame2Box,
clear // special case to remove all boxes
}
void Start()
{
// Set up an array so our script can more easily set up the hit boxes
colliders = new PolygonCollider2D[]{frame2};
// Create a polygon collider
localCollider = gameObject.AddComponent<PolygonCollider2D>();
localCollider.isTrigger = true; // Set as a trigger so it doesn't collide with our environment
localCollider.pathCount = 0; // Clear auto-generated polygons
}
void OnTriggerEnter2D(Collider2D col)
{
Debug.Log("Collider hit something!");
}
public void setHitBox(hitBoxes val)
{
if(val != hitBoxes.clear)
{
localCollider.SetPath(0, colliders[(int)val].GetPath(0));
return;
}
localCollider.pathCount = 0;
}
}
And here is the health script.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class HealthScript : MonoBehaviour {
private GameMaster gm;
public bool isEnemy = true;
public float max_Health = 100f;
public float cur_Health = 0f;
public GameObject healthBar;
void Start()
{
gm = GameObject.FindGameObjectWithTag ("GameMaster").GetComponent<GameMaster> ();
cur_Health = max_Health;
InvokeRepeating ("decreaseHealth", 1f, 1f);
}
void Update()
{
}
void decreaseHealth()
{
//uncomment if you want to test health bar.
float calc_Health = cur_Health / max_Health;
SetHealthBar (calc_Health);
}
public void SetHealthBar(float myHealth)
{
healthBar.transform.localScale = new Vector3 (myHealth, healthBar.transform.localScale.y, healthBar.transform.localScale.z);
}
// Update is called once per frame
void OnTriggerEnter2D (Collider2D collider)
{
HeroHitBoxManager hit = collider.gameObject.GetComponent<HeroHitBoxManager> ();
hit = null;
if (hit != null && gameObject.tag == "Enemy")
{
cur_Health = cur_Health - 5;
}
if (cur_Health <= 0 && gameObject.tag == "Enemy") {
Destroy (gameObject);
gm.points -= 1;
if (gm.points == 0){
SceneManager.LoadScene("WinScreen");
}
}
if(cur_Health <= 0 && gameObject.tag == "Player"){
Application.LoadLevel("YouLose");
}
}
void OnCollisionEnter2D (Collision2D col)
{
//Check collision name
Debug.Log("collision name = " + GetComponent<Collider2D>().gameObject.name);
if(col.gameObject.tag == "Enemy")
{
cur_Health = cur_Health - 5;
}
}
}
Comment
It's because all enemies that enter the collider will get hit. You have to set a bool variable to keep track of if the sword has hit something yet, and if it has, then don't process anymore attacks.
Answer by starrtennis · Aug 07, 2019 at 01:00 AM
This sounds like a health script issue, not a polygon collider issue. Is your health a static variable by mistake?