#So I could use some input on a enemy

1 messages · Page 1 of 1 (latest)

spiral wren
#

I'm having some issues with the angles the enemy is spotting the player from

#

see the pictures below:

#

the green line indicates that the enemy can see the player

#

I want the player to be spotted when inside the yellow lines on picture 2

#

it shouldn't be spotted on picture 1

#

also, the FOV is pointing upwards when I want it to point forwards

#

I didn't write the code myself, just made some changes to convert it to "2D"

#

here are the two scripts:

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FieldOfView : MonoBehaviour
{
    public float radius;
    [Range(0,360)]
    public float angle;

    public GameObject playerRef;

    public LayerMask targetMask;
    public LayerMask obstructionMask;

    public bool canSeePlayer;

    private void Start()
    {
        playerRef = GameObject.FindGameObjectWithTag("Player");
        StartCoroutine(FOVRoutine());
    }

    private IEnumerator FOVRoutine()
    {
        WaitForSeconds wait = new WaitForSeconds(0.2f);

        while (true)
        {
            yield return wait;
            FieldOfViewCheck();
        }
    }

    private void FieldOfViewCheck()
    {
        Collider[] rangeChecks = Physics.OverlapSphere(transform.position, radius, targetMask);

        if (rangeChecks.Length != 0)
        {
            Transform target = rangeChecks[0].transform;
            Vector3 directionToTarget = (target.position - transform.position).normalized;

            if (Vector3.Angle(transform.forward, directionToTarget) < angle / 2)
            {
                float distanceToTarget = Vector3.Distance(transform.position, target.position);

                if (!Physics.Raycast(transform.position, directionToTarget, distanceToTarget, obstructionMask))
                    canSeePlayer = true;
                else
                    canSeePlayer = false;
            }
            else
                canSeePlayer = false;
        }
        else if (canSeePlayer)
            canSeePlayer = false;
    }
}```
#

and the editor script:

#
using UnityEngine;

[CustomEditor(typeof(FieldOfView))]
public class FieldOfViewEditor : Editor
{
    private void OnSceneGUI()
    {
        FieldOfView fov = (FieldOfView)target;
        Handles.color = Color.white;
        Handles.DrawWireArc(fov.transform.position, Vector3.forward, Vector3.right, 360, fov.radius);

        Vector3 viewAngle01 = DirectionFromAngle(fov.transform.eulerAngles.z, -fov.angle / 2);
        Vector3 viewAngle02 = DirectionFromAngle(fov.transform.eulerAngles.z, fov.angle / 2);

        Handles.color = Color.yellow;
        Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngle01 * fov.radius);
        Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngle02 * fov.radius);

        if (fov.canSeePlayer)
        {
            Handles.color = Color.green;
            Handles.DrawLine(fov.transform.position, fov.playerRef.transform.position);
        }
    }

    private Vector3 DirectionFromAngle(float eulerY, float angleInDegrees)
    {
        angleInDegrees += eulerY;

        return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad), 0);
    }
}```
glacial juniper
#

But u want to work in 3d or in 2d?

spiral wren
#

3D, but seen from the side

glacial juniper
#

the easiest way was to convert the field of view to 3d

spiral wren
#

the fov used to be in 3D, but since its a sidescroller I didn't really need it to check for things in the Z direction

#

so how it works now is how I want it to work, I just cant get the angles right

glacial juniper
#

hmmm okok

full shard
#

honestly I can't even tell where "forward" is for the red object (if that is an enemy)

spiral wren
#

rotation is 0 0 0, so forward should be Right

#

and yes the red object is the enemy

glacial juniper
#

if u invert the direction of the angle give by DirectionFromAngle

#

dont work?

spiral wren
#

whats the best way to invert the angle?

glacial juniper
#

check this video

#

it's the same that I used some moths ago

spiral wren
#

i'm so close to making the script I have work, so would be great to sort it out 🙂

#

thanks for your link though!

glacial juniper
#

If I understand well

full shard
#

when you rotate it along the vertical axis do the yellow fov indicators rotate smoothly around the circle in the expected direction?

spiral wren
#

yes

#

or no

full shard
#

and when it starts by facing right, you would expect the yellow indicators to point right as well? (so long as angle < 90)

spiral wren
#

hold on

#

yes it rotates smoothly

#

Z rotation is -90 there

full shard
#

wait, don't you want to base it on Y rotation tho?

spiral wren
#

... ah

#

I think you found an issue 😉

#

I set it up in the wrong rotation from the start

#

but the ground is 0 0 0 rotation

#

im confused

#

sry im super new to this

spiral wren
#

ok if I set the rotation to 0 90 90 I get exactly what I want but inverted

full shard
#

the FOV detection was actually flawless (though I forgot to mask & tag player properly and was wondering wtf was going on for a while), it was just the editor script that needed changing

#
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(FieldOfView))]
public class FieldOfViewEditor : Editor
{
    private void OnSceneGUI()
    {
        FieldOfView fov = (FieldOfView)target;
        Handles.color = Color.white;
        Handles.DrawWireArc(fov.transform.position, Vector3.up, Vector3.right, 360, fov.radius);

        Vector3 viewAngle01 = DirectionFromAngle(fov.transform.eulerAngles.y, -fov.angle / 2);
        Vector3 viewAngle02 = DirectionFromAngle(fov.transform.eulerAngles.y, fov.angle / 2);

        Handles.color = Color.yellow;
        Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngle01 * fov.radius);
        Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngle02 * fov.radius);

        if (fov.canSeePlayer)
        {
            Handles.color = Color.green;
            Handles.DrawLine(fov.transform.position, fov.playerRef.transform.position);
        }
    }

    private Vector3 DirectionFromAngle(float eulerY, float angleInDegrees)
    {
        angleInDegrees += eulerY;

        return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
    }
}
spiral wren
#

ok so you changed it back to 3D

#

I dont really need a depth radius since its a sidescroller

#

Its actually working as i want when setting the rotation to 0 90 90 and the player is looking to the right

spiral wren
#

ok I got it working, needed to change it to DirectionFromAngle(fov.transform.eulerAngles.y