- Home /
Question by
Ezecrafting_YT · Feb 18, 2021 at 01:42 AM ·
c#3daicollision detection
Need help with Collider detection with Physics.OverlapSphere()
So, in my game I have NavMehsAgents for my NPCs, and they work comepletely fine, except for the barrier detection part, which should make them go to the closest barrier, and hit it one time.
This is the script, but just including what the functions need to work, since I don't want to flood my entire post:
[Header("Movement")]
public float hearRadius;
public float speed;
[Min(0f)]
public float pauseTime;
public int strength;
[Header("NavMesh")]
public NavMeshAgent agent;
Transform player;
public LayerMask whatIsGround, whatIsPlayer;
bool canWander = true;
Vector2 smoothDeltaPosition = Vector2.zero;
Vector2 velocity = Vector2.zero;
[Header("Walking")]
public Vector3 walkPoint;
bool walkPointSet;
public float walkPointRange;
public Collider barrierDetection;
[Header("Visual Tweaks")]
public Animator anim;
public void CheckForBarriers()
{
Debug.Log("Sent to check for layers!");
GetComponentInChildren<BarrierDetection>().CheckBarrier();
}
IEnumerator makeAtt(GameObject attObj)
{
yield return null;
}
public void GoToClosestBarrier()
{
Collider[] barriers = Physics.OverlapSphere(transform.position, 150f, LayerMask.NameToLayer("barrierMask"), QueryTriggerInteraction.Collide);
StartCoroutine(gotoBarrier(barriers[0].gameObject));
}
IEnumerator gotoBarrier(GameObject attObj)
{
yield return null;
canWander = false;
followPlayer = false;
agent.SetDestination(attObj.transform.position);
Vector3 distanceToBarrier = transform.position - attObj.transform.position;
while(distanceToBarrier.magnitude > 1.1f)
{
distanceToBarrier = transform.position - attObj.transform.position;
}
if (distanceToBarrier.magnitude <= 1f)
{
StartCoroutine(actuallyAttack(attObj));
}
}
IEnumerator actuallyAttack(GameObject attObj)
{
canWander = false;
transform.LookAt(attObj.transform.forward);
anim.SetBool("att", true);
attObj.GetComponent<BarrierController>().Damage(strength);
yield return new WaitForSeconds(1f);
anim.SetBool("att", false);
canWander = true;
}
this is my BarrierController:
public int health = 15;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Damage(int val)
{
health -= val;
Debug.Log("Got Hit!");
}
and finally here's my BarrierDetection script: public enum charType { Player, NPC };
public charType characterType;
GameObject parent;
GameObject player;
Vector3 attPos;
GameObject barrierObj;
bool isBarrierThere;
// Start is called before the first frame update
void Start()
{
parent = transform.parent.gameObject;
player = GameObject.FindWithTag("Player");
}
// Update is called once per frame
void Update()
{
}
public void CheckBarrierNPC()
{
if (isBarrierThere)
{
Debug.LogWarning("Attacking as NPC...");
parent.GetComponent<NPCController>().Attack(barrierObj);
}
else
{
Debug.LogWarning("Leaving as NPC...");
parent.GetComponent<NPCController>().FuckGoBack();
}
}
public void CheckBarrier()
{
Debug.Log("Checking Barrier...");
if (isBarrierThere)
{
Debug.LogWarning("Attacking as Player...");
parent.GetComponent<PlayerController>().Attack(barrierObj);
}
}
/*void OnColisionStay(Collider other)
{
if (other.gameObject.CompareTag("Barrier"))
{
attPos = other.transform.position;
barrierObj = other.gameObject;
isBarrierThere = true;
}
}
void OnColisionExit(Collider other)
{
if (other.gameObject.CompareTag("Barrier"))
{
isBarrierThere = false;
}
}*/
void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("Barrier"))
{
attPos = other.transform.position;
barrierObj = other.gameObject;
isBarrierThere = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Barrier"))
{
isBarrierThere = false;
}
}
I have tried to fix this for 3 days straight and I'm in a week long game jam so please help me!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613025347im_/https://answers.unity.com/themes/thub/images/avi.jpg)