Hello, I am making a game in which your goal is to collect pages whilst avoiding an enemy.
In order to check for the pages, and if the player can see the enemy, I use two different raycast scripts. Both work perfectly on their own, but for some reason, when I collect exactly two pages, the enemy detection starts acting inconsistently (the page detection works fine however)
(POTENTIAL EPILEPSY WARNING, fast scrolling bars, apologies if it is straining, I haven’t finalized the effect yet)
if (GeometryUtility.TestPlanesAABB(planes, slenderMan.GetComponent<CapsuleCollider>().bounds))
{
if (Physics.CheckSphere(transform.position, 100))
{
Collider[] hitColliders = Physics.OverlapSphere(transform.position, 50, enemyMask);
inProximity = true;
if (hitColliders.Length != 0)
{
Transform target = hitColliders[0].transform;
Vector3 targetDirection = ((target.position - transform.position).normalized);
Vector3 rDirection = ((target.position - rSide.transform.position).normalized);
Vector3 lDirection = ((target.position - lSide.transform.position).normalized);
if (Vector3.Angle(player.transform.position, targetDirection) < angle / 2)
{
float targetDistance = Vector3.Distance(transform.position, target.position);
if (!Physics.Raycast(transform.position, targetDirection, targetDistance, walls))
{
exposed = true;
}
else if (!Physics.Raycast(lSide.transform.transform.position, lDirection, targetDistance, walls))
exposed = true;
else if (!Physics.Raycast(rSide.transform.transform.position, rDirection, targetDistance, walls))
exposed = true;
else
exposed = false;
}
}
}
}
else if (exposed == true)
exposed = false;
else if (inProximity == true)
inProximity = false;
RaycastHit hit;
if (Physics.Raycast(mainCamera.transform.position, mainCamera.transform.TransformDirection(Vector3.forward),out hit, 2f))
{
if (hit.collider.tag == "Page")
{
handIcon.enabled = true;
Debug.DrawRay(mainCamera.transform.position, mainCamera.transform.TransformDirection(Vector3.forward));
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Destroy(hit.transform.gameObject);
pageSound.Play();
pages++;
StartCoroutine(showText());
handleAmbience();
return;
}
}
else
{
handIcon.enabled = false;
}
}
else
{
handIcon.enabled = false;
}
The first script is for enemy detection, second is for pages. I’m not very experienced at coding, so If anyone could explain why I’m having this problem or how I could fix it, I’d appreciate it greatly.