Guard AI acting real weird - Please Help!
Hello! I am still very new to this whole Unity thing but I'm absolutely loving it. I have encountered a big problem in my first 2d game with my Guard AI. The game design is very simple; guide the little square (player) across the platforms in the level to reach the end, with many different Guard AI spread across it shooting out a red ray that, if hit with the player, causes game over. My main Guard AI is quite simple as well - move back and forth across a single platform, when getting to the end of the platform turning around and shooting out a red beam that covers the length of the platform, causing the player to have to jump up when it shoots to avoid getting hit, with the red beam then disappearing and it moving back across to the other side of the platform. For some odd reason upon touching down to the platform anywhere to the left of the Guard, even when the beam is not out, the beam automatically and instantly shoots out and kills the player. I have been working at this problem for weeks but my simple lack of coding skills has not allowed me to locate the cause of this. I have attached the entire Guard script here:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Guard2 : MonoBehaviour {
public Transform pathHolder;
Transform player;
Color originalSpotLightColour;
public float distance;
public LineRenderer lineOfSight;
public Gradient redColor;
public Gradient greenColor;
public float speed = 5;
public float waitTime = .3f;
public float turnSpeed = 90;
public Light spotLight;
public float viewDistance;
public LayerMask viewMask;
float viewAngle;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
viewAngle = spotLight.spotAngle;
originalSpotLightColour = spotLight.color;
Vector3[] waypoints = new Vector3[pathHolder.childCount];
for (int i = 0; i < waypoints.Length; i++)
{
waypoints[i] = pathHolder.GetChild(i).position;
waypoints[i] = new Vector3(waypoints[i].x, transform.position.y, waypoints[i].z);
}
StartCoroutine(FollowPath(waypoints));
}
void Update()
{
if (CanSeePlayer())
{
spotLight.color = Color.red;
}
else
{
spotLight.color = originalSpotLightColour;
}
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.right, distance);
if (hitInfo.collider != null)
{
Debug.DrawLine(transform.position, hitInfo.point, Color.red);
lineOfSight.SetPosition(1, hitInfo.point);
lineOfSight.colorGradient = redColor;
if (hitInfo.collider.CompareTag("Player"))
{
Destroy(hitInfo.collider.gameObject);
}
}
else
{
Debug.DrawLine(transform.position, transform.position + transform.right * distance, Color.green);
lineOfSight.SetPosition(1, transform.position + transform.right * distance);
lineOfSight.colorGradient = greenColor;
}
lineOfSight.SetPosition(0, transform.position);
}
bool CanSeePlayer()
{
if (Vector3.Distance(transform.position, player.position) < viewDistance)
{
Vector3 dirToPlayer = (player.position - transform.position).normalized;
float angleBetweenGuardAndPlayer = Vector3.Angle(transform.forward, dirToPlayer);
if (angleBetweenGuardAndPlayer < viewAngle / 2f)
{
if (!Physics.Linecast(transform.position, player.position, viewMask))
{
return true;
}
}
}
return false;
}
IEnumerator FollowPath(Vector3[] waypoints)
{
transform.position = waypoints[0];
int targetWaypointIndex = 1;
Vector3 targetWayPoint = waypoints[targetWaypointIndex];
transform.LookAt(targetWayPoint);
while (true)
{
transform.position = Vector3.MoveTowards(transform.position, targetWayPoint, speed * Time.deltaTime);
if (transform.position == targetWayPoint)
{
targetWaypointIndex = (targetWaypointIndex + 1) % waypoints.Length;
targetWayPoint = waypoints[targetWaypointIndex];
yield return new WaitForSeconds(waitTime);
yield return StartCoroutine(TurnToFace(targetWayPoint));
}
yield return null;
}
}
IEnumerator TurnToFace(Vector3 lookTarget)
{
Vector3 dirToLookTarget = (lookTarget - transform.position).normalized;
float targetAngle = 90 - Mathf.Atan2(dirToLookTarget.z, dirToLookTarget.x) * Mathf.Rad2Deg;
while (Mathf.Abs(Mathf.DeltaAngle(transform.eulerAngles.y, targetAngle)) > 0.05f)
{
float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetAngle, turnSpeed * Time.deltaTime);
transform.eulerAngles = Vector3.up * angle;
yield return null;
}
}
void OnDrawGizmos()
{
Vector3 startPosition = pathHolder.GetChild(0).position;
Vector3 previousPosition = startPosition;
foreach (Transform waypoint in pathHolder)
{
Gizmos.DrawSphere(waypoint.position, .3f);
Gizmos.DrawLine(previousPosition, waypoint.position);
previousPosition = waypoint.position;
}
Gizmos.DrawLine(previousPosition, startPosition);
Gizmos.color = Color.red;
Gizmos.DrawRay(transform.position, transform.forward * viewDistance);
}
}
I have many video visuals of this problem that are too large to attach here - if anyone would like to communicate via email to get a visual I would love to send them over! Thanks for taking the time to read and if anyone has any tips/ suggestions to solve this problem please let me know either here or email me at rsrickard@yahoo.com