- Home /
The question is answered, right answer was accepted
Gameobject not registering collisions
Hello.
I am have a small problem. I have currently switched to Unity 2018.2.1f.
In my game the player can attack using various weapons. The way my player deals damage is by a cube that is placed infront of the player ( Damager ) with box collider set to triger.My enemy has capsule collider set to triger.
My problem is when the enemy collides with the Damager gameobject no damage gets applied.
Here is my script for the Damager gameobject.
Hrac_WEAPONMANAGER hrcWeaponManager;
public float damageToDeal;
BoxCollider damageCollider;
public Material colliderOFF;
public Material colliderON;
Material currentMaterial;
private void Start()
{
damageCollider = GetComponent<BoxCollider>();
damageCollider.enabled = false;
currentMaterial = GetComponent<Renderer>().material;
currentMaterial.color = colliderOFF.color;
}
// Update is called once per frame
void Update ()
{
if(GetComponentInParent<Hrac_WEAPONMANAGER>() != null)
{
hrcWeaponManager = GetComponentInParent<Hrac_WEAPONMANAGER>();
damageToDeal = hrcWeaponManager.damage;
}
}
public void colliderActivator()
{
StartCoroutine(colliderActive());
}
IEnumerator colliderActive()
{
damageCollider.enabled = true;
currentMaterial.color = colliderON.color;
yield return new WaitForSeconds(0.5f);
currentMaterial.color = colliderOFF.color;
damageCollider.enabled = false;
}
}
and here is the script for enemy health
public float maxHealth;
public float currentHealth;
void Start ()
{
currentHealth = maxHealth;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "DAMAGER")
{
Hrac_DAMAGER hrcDamager = other.GetComponent<Hrac_DAMAGER>();
currentHealth = currentHealth - hrcDamager.damageToDeal;
}
}
void Update()
{
if (currentHealth <= 0f)
{
Destroy(gameObject);
}
}
}
This code is taken from my older project and everything works perfectly with no problems on older versions of Unity. Does anybody knows what might be the problem ?
First issue: maybe the code is missing, but I'm not seeing anything that's calling 'colliderActivator()'.
Answer by Carterryan1990 · Jul 31, 2018 at 01:00 AM
Does your object have a rigidbody? If not add one and Set Iskinematic to true. For collisions and triggers to take place and register one of the objects needs to have a rigidbody.
I am an Idi*t. $$anonymous$$y object didn't had rigidbody.No wonder it didn't work. :D
Thank you !
Follow this Question
Related Questions
Trigger and ennemy damage 2 Answers
Child Object's collider setting off triggers 1 Answer
Inconsistent Trigger events 1 Answer