Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyVision : MonoBehaviour
{
    private Enemy enemyScript;
    public GameObject playerCenter;
    public float range = 5.2f;
    public float fieldOfView = 45f;

    private float distanceToPlayer;
    private float angleToPlayer;
    private Vector3 targetDirection;
    private RaycastHit whatHasBeenHit;


    private void Start()
    {
        enemyScript = GetComponent<Enemy>();
        if (playerCenter == null)
        {
            playerCenter = GameObject.FindWithTag("Player");
        }
    }
    
    void Update()
    {
        targetDirection = playerCenter.transform.position - this.transform.position;
        //
        angleToPlayer = Vector3.Angle(targetDirection, transform.forward);
        //
        distanceToPlayer = Vector3.Distance(playerCenter.transform.position, this.transform.position);
        //
        Debug.DrawRay(this.transform.position, transform.forward * range, Color.green, 1, true);
        //

        //
        if (angleToPlayer < fieldOfView && distanceToPlayer < range )
        {
            //

            //
            if (Physics.Raycast(this.transform.position, targetDirection, out whatHasBeenHit, range))
            {
                //
                if (whatHasBeenHit.collider.gameObject.tag == "Player")
                {
                    Debug.DrawRay(transform.position, targetDirection, Color.red, 1, true);
                    Debug.Log("seen player");
                    //
                    enemyScript.state = 2;
                }
                else 
                {
                    //
                    enemyScript.state = 1;
                }
            }   
        }
    }
} 


What I have tried:

If you see any not needed lines or ways to make it more efficient.

Last time i posted here i got very helpful answers, so thank you for that.
Posted
Updated 7-Jun-22 21:21pm
v2
Comments
PIEBALDconsult 7-Jun-22 21:02pm    
Avoid ifs, particularly string comparisons.
Member 15627495 8-Jun-22 1:40am    
as easy fix,

you can delete the 4 first instructions in void update()
and migrate the calculation straight to it, in the code under.

It's a tiny gain but again.

I explain :
you write :
A = B + C
to use A after
if ( A < x ) {....}
you did calculate, allocate, and re-use A.

just go on :
if( ( B+C ) < x ) { .... }

you're working by C# , it will pass, C# is good recurser handler, so this kind of calls are compliant.
It's a common 'writing code lines' fault.

why don't you use memory address to call you var , and so references ? ( here a big velocity gain by direct access to memory ),
sure it's a different syntax, but closer to high speed ) C# C# C# C#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900