Hey guys,
In my research I’ve noticed a lot of posts starting ‘I’m a total noob with c#’ and this is no different. I have zero experience with C#, nor any aptitude for it at all, but I’ve been tasked with creating a simple 2D game for University.
In my game, the player controls a discarded lollipop that needs to get back to the safety of the ice cooler before being melted by the sun. For this to happen I’ve been trying to set up a field of view for the sun, and used a tutorial on YouTube. The tutorial was intended to create a 360 degree field of view, and enable the enemy to detect the player, it then also guided me through how to create gizmos to reflect all that in the viewport.
In the image above you can see the gizmos displaying the field of view, and the code should also draw a line between the circle/sun, and the player. Somewhere, something has gone wrong. I’m not sure if it’s the raycast, or collision, or something else. And I’m not familiar enough with the code to figure it out.
Once complete, the tutorial demonstrated the working code by directing me into debug, where I could see that the ‘Can See Player’ tickbox is checked. But here, nothing happens, as you can see in the picture below.
I would greatly appreciate some insights here.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FieldOfView : MonoBehaviour
{
public float radius = 5f;
[Range(1, 360)]public float angle = 180f;
public LayerMask targetLayer;
public LayerMask obstructionLayer;
public GameObject playerRef;
public bool CanSeePlayer { get; private set; }
void Start()
{
playerRef = GameObject.FindGameObjectWithTag("Player");
StartCoroutine(FOVCheck());
}
private IEnumerator FOVCheck()
{
WaitForSeconds wait = new WaitForSeconds(0.2f);
while (true)
{
yield return wait;
FOVCheck();
}
}
private void FOV()
{
Collider2D[] rangeCheck = Physics2D.OverlapCircleAll(transform.position, radius, targetLayer);
if (rangeCheck.Length > 0)
{
Transform target = rangeCheck[0].transform;
Vector2 directionToTarget = (target.position - transform.position).normalized;
if (Vector2.Angle(transform.up, directionToTarget) < angle / 2)
{
float distanceToTarget = Vector2.Distance(transform.position, target.position);
if (!Physics2D.Raycast(transform.position, directionToTarget, distanceToTarget, obstructionLayer))
CanSeePlayer = true;
else
CanSeePlayer = false;
}
else
CanSeePlayer = false;
}
else if (CanSeePlayer)
CanSeePlayer = false;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.white;
UnityEditor.Handles.DrawWireDisc(transform.position, Vector3.forward, radius);
Vector3 angle01 = DirectionFromAngle(-transform.eulerAngles.z, -angle / 2);
Vector3 angle02 = DirectionFromAngle(-transform.eulerAngles.z, angle / 2);
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position, transform.position + angle01 * radius);
Gizmos.DrawLine(transform.position, transform.position + angle02 * radius);
if (CanSeePlayer)
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, playerRef.transform.position);
}
}
private Vector2 DirectionFromAngle(float eulerY, float angleInDegrees)
{
angleInDegrees += eulerY;
return new Vector2(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
}