- Home /
The question is answered, right answer was accepted
Replacing OverlapSphere with colliders (Detecting weapon collision with enemies - SOLVED)
So i basically have this really basic combat system that i made off Brackeys' tutorials...but now i want it to be more complex and involve colliders instead of Physics.OverlapSphere(). So i just need to ask how do i create a replacement for this single line of code
Collider[] hitEnemies = Physics.OverlapSphere(attackpoint.position, attackradius, enemies);
but this time using colliders. just to clarify, I intend to make a combat system where the weapon has two colliders -- One on the Main Attacking part and another on other parts which will deal significantly less damage. So i have divided the mesh into two parts giving them separate colliders.
It would be great if you could just help out with the first issue though i think i can figure the multiple colliders out myself. Big Thanks in Advance.
Here is my messy combat script for reference
EDIT: I managed to get a few things done that i think should've worked. I have updated my player combat script. However, things seem to be not working too well...instead of interacting with the enemies...the weapon is pushing them around. I want the weapon to phase through the enemy collider.
the scripts attached to my weapons are
This is the WeaponCollider script to manage the different collisions
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponCollider : MonoBehaviour
{
public Collider maincollider;
public Collider secondarycollider;
public PlayerCombat playercombat;
public bool maincollides;
public bool secondarycollides;
void Update()
{
if(!maincollides && !secondarycollides)
{
playercombat.hitEnemies.Clear();
}
}
}
The script attached to my main collider (the one that deals more damage) is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainCollision : MonoBehaviour
{
public WeaponCollider collidermanager;
public bool checking;
public Collider enemytocheck;
public bool result;
void OnTriggerEnter(Collider collider)
{
collidermanager.maincollides = false;
if(collider.gameObject.layer == LayerMask.GetMask("enemy"))
{
collidermanager.playercombat.hitEnemies.Add(collider);
}
}
void OnTriggerStay(Collider collider)
{
if(checking)
{
if(enemytocheck == collider)
{
result = true;
ReturnCheck();
}
else
{
result = false;
ReturnCheck();
}
}
}
void OnTriggerExit(Collider collider)
{
collidermanager.maincollides = false;
}
public void CheckEnemy(Collider enemy)
{
checking = true;
enemytocheck = enemy;
}
public bool ReturnCheck()
{
return (result);
}
}
The script attached to my secondary collider is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SecondaryCollision : MonoBehaviour
{
public WeaponCollider collidermanager;
void OnTriggerEnter(Collider collider)
{
collidermanager.secondarycollides = false;
if(collider.gameObject.layer == LayerMask.GetMask("enemy"))
{
collidermanager.playercombat.hitEnemies.Add(collider);
}
}
void OnTriggerExit(Collider collider)
{
collidermanager.secondarycollides = false;
}
}
The maxdamage and mindamage variables in the combat script are the damage values for the main and secondary collider respectively. Please Help!!
Answer by xxmariofer · Jul 01, 2021 at 07:43 AM
the collider itself in the editor has a property called "is Trigger" you need to click it
I ticked this property on the weapon colliders a few $$anonymous$$utes after making this edit....not the weapon can pass through enemies but it still doesn't interact.....is there a problem in my code?
there could be multiple reasons why it is not working, take into account this notes:
A, At least one of the objects must be with the istrigger ticked
B, Atleast 1 object needs a rigidbody to triger events to fireç
C, make sure enemies do have the enemy layer setup
No dice! I think it's a problem with my code...but i can't figure out what...plus the current code i made can process only one enemy at a time....ill try to add a list with the main collider and try...i would be grateful if you could find the anomaly
Follow this Question
Related Questions
Wheel Colliders not colliding with Mesh Collider 0 Answers
WebGL Build bugged Colliders 0 Answers
basically i am having collider issues and idk wat to do 1 Answer
Slenderman script problem 5 Answers
OverlapCircleAll, layerMask ( Problem) 2 Answers