How to make enemy kill you if starring at it?

How exactly to make an enemy kill you if loking at it? Actually like in slender (no copy of the game) just want to use it in another kind a game.

One way of solving this problem would be to compare two vectors:

  • The direction of the enemy.
  • The direction that the player is looking.

First find and normalize the direction of the enemy from the player.

Now compare that to the view direction with a dot product. A dot product of two normalized vectors will return 1 if they are parallel in the same direction, and -1 if they are parallel but going in opposite directions. Otherwise will return a number between those two depending on how wide the angle between them is.

Now you can check that dot product against a predefined margin. If the player is looking at the enemy, start incrementing a counter. When the counter exceeds a certain time kill the player.

UPDATE:

Now there is a minimum distance by which the player is affected by looking at the enemy.
Additionally, there is a check to see if something is blocking your view of the enemy. This might need tweeking, because it’s just a single raycast from the center of the player to the center of the enemy. That means if a small object was sitting between you and the enemy, but not actually obscuring the entire enemy, it would still consider the enemy hidden. But hopefully you guys get the idea.

Comment for questions.

UPDATE:

Fixed some errors in the code. Renamed script to “EnemyDetector”.

using UnityEngine;

public class EnemyDetector : MonoBehaviour {
    // The enemy
    public Transform enemy;
    // How wide is the area considered to be looking at the enemy?
    public float lookWidth = 0.1f;
    // How long can the player look before dying? (seconds)
    public float maxLookDuration = 2.0f;
    // Minimum distance to enemy transform.
    public float lookDistance = 10f;
    // How quickly does counter fall go back down to zero when not looking at enemy?
    public float counterFalloff = 1f;
    // How long the player has been looking at the enemy.
    private float lookDuration = 0.0f;

    void Update() {
        // Vector from player to enemy.
        Vector3 enemyVector = enemy.position - transform.position;

        // Check if enemy is in range, and if looking at enemy.
        if (enemyVector.magnitude <= lookDistance &&
            IsVectorInLookWidth(enemyVector) &&
            IsObscured(enemy) == false) {

            // Player is looking at enemy, increment counter.
            lookDuration += Time.deltaTime;

            // Player has looked at enemy for too long, die.
            if (lookDuration > maxLookDuration) {
                Die();
            }
        } else if (lookDuration > 0f){
            // Player is not looking at enemy, allow counter to drop back down.
            lookDuration = Mathf.Max(lookDuration - Time.deltaTime * counterFalloff, 0f);
        }

		Debug.Log("look duration = " + lookDuration);
    }

    bool IsVectorInLookWidth(Vector3 vector) {
        // Check if line falls within given viewWidth from forward vector.
        return Vector3.Dot(vector.normalized, transform.forward) > (1.0f - lookWidth);
    }

    bool IsObscured(Transform target) {
        // Check line of sight to target.
        // If a raycast from viewer to target hits something else inbetween, it is considered obscured.
        // This will hit any collider, so a layermask may be needed.
        RaycastHit hit;
        if (Physics.Linecast(transform.position, target.position, out hit)) {
            // The ray has hit the enemy. It can be seen!
            if (hit.transform == target) return false;
        }
        // The ray has hit some other thing (or nothing at all)
        return true;
    }

    void Die() {
        Destroy(gameObject);
    }
}

Please explain step by step introduction to the script in the project, as I have tried in my project and it did not work.
I appreciate the attention.