13.2 C
New York
Friday, December 19, 2025

unity – I have to entry the “canSeePlayer” property within the playerController script from enemies script


II have to entry the canSeePlayer property within the playerController script from enemies script. I’ve tried as a lot as attainable however to no avail. I have to code it such that I can use it even when there’s a number of objects with the identical code I can use it. I’m utilizing a easy ray forged to examine if the participant is in a set off space or not.

enemies code:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing TMPro;
utilizing UnityEngine;

public class enemies : MonoBehaviour
{
    public GameObject playerRef;
    public LayerMask targetLayer;
    public LayerMask obstructionLayer;
    public bool canSeePlayer { get; personal set; }

    personal void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Bullets"))
        {
            Destroy(gameObject);
        }
    }

    personal void Begin()
    {
        playerRef = GameObject.FindGameObjectWithTag("Participant");
        //StartCoroutine(FOVcheck());
    }

    personal void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("Participant"))
        {
            Vector2 dicrectionToTarget = (playerRef.remodel.place - remodel.place).normalized;
            float distanceToTarget = Vector2.Distance(remodel.place, playerRef.remodel.place);
            canSeePlayer = !Physics2D.Raycast(remodel.place, dicrectionToTarget,distanceToTarget, obstructionLayer);
        }
    }

    personal void OnTriggerExit2D(Collider2D collision)
    {
        if(canSeePlayer)
        {
            canSeePlayer = false;
        }
    }
}

and right here is the playerController:

utilizing UnityEngine;
utilizing UnityEngine.Rendering;

public class playerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    public Weapon weapon;
    Vector2 moveDirection;
    Vector2 mousePosition;

    public void Replace()
    {
        if (Enter.GetMouseButtonDown(0))
        {
            weapon.Fireplace();
        }
    }

    personal void FixedUpdate()
    {
        float moveX = Enter.GetAxisRaw("Horizontal");
        float moveY = Enter.GetAxisRaw("Vertical");
        moveDirection.x = moveX;
        moveDirection.y = moveY;
        mousePosition = Digicam.essential.ScreenToWorldPoint(Enter.mousePosition);
        rb.linearVelocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed);
        Vector2 aimDirection = mousePosition - rb.place;
        float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f;
        rb.rotation = aimAngle;
    }
}

I’ve tried to attach the 2 scripts utilizing public enemies Enemies; however that’s just for a single prefab so it will not work. I’m not certain what else I can do to make it work and I’m quite misplaced.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles