- Home /
Need help detecting barriers for my game
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!
EDIT:
By the way I let in the commented functions to see if they are the ones I shoud let in there. And, here are some things to note:
have two colliders on my barriers: One that is the actual barrier and another one that is for collision detection (the second one has a trigger collider)
Your answer
Follow this Question
Related Questions
is it possible to use navmesh on flying character and target ? 0 Answers
Editing NavMesh Paths? 0 Answers
NavMeshAgent resume original position and facing direction C# 0 Answers
My ai is getting stuck when there is a lot of them 0 Answers
How to get a velocity unit vector from a NavMeshAgent? 1 Answer