- Home /
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.
What POV? First-person, third-person, or something else?
Slender is a first person game. So I assume he means the same.
Answer by rhys_vdw · Sep 19, 2012 at 07:27 AM
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);
}
}
copyied and pasted this and got an error: Assets/Player.cs(16,71): error CS1061: Type `UnityEngine.Vector3' does not contain a definition for `position' and no extension method `position' of type `UnityEngine.Vector3' could be found (are you missing a using directive or an assembly reference?)
???
I didn't test that one out. There were a couple of errors in there. It's been fixed now. Note that this will only work if it's attached to the Camera that the player is seeing through.
Also you might want to do something a bit more exciting in the Die method.
Hope it helps.
it works fantastic now, set it to kill player if looking at enemy 10 sec. Thanks alot XD youre right about something more exciting in the death,and are going to find something know. again thanks alot :)
hey can you help me guys? when i put the script into the main camera i got this message/error "Can't add script" "Cant add script behaviour $$anonymous$$ainCamera the scripts file does not match the name of the class defined in the script" how am i supposed to name the script? or just tell me what to do please. also i cant find the "kill player if looking at enemy..." where is the line? and is there "seconds" already set up for dying? sorry for english.
It works perfectly, but there's one problem. So Im makign a slender-like game, and I have the First Person Controller across the map from the Slender Enemy. And whenever I start the game and look in the direction of Slender, I die. How do I adjust the distance of the viewing angle?
Answer by Joselicious · Apr 22, 2013 at 02:30 PM
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.
Step 1 : read this page : http://answers.unity3d.com/page/newuser.html
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
Here at Unity Answers, Answer means Solution, not Response.
Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
enemy spawn and in game time counter 1 Answer
Trap Door Question 1 Answer
Enemy AI Help? 1 Answer