- Home /
If Player Looks At Enemy Script
Hey guys, i've tried alot of things but i can't get it to work. In my script it measures the distance between the enemy and the player, if the player is close to the enemy and he/she looks at the enemy something happends. And that is what i can't get to work. I'm sorry if this is big ask, but i've been searching and testing scripts for a long time now and without sucesss. Thanks in advance.
if (dist < 20 && *looking at enemy*)
{
debug.log("Play an audio track that pauses so it doesn't repeat right after being looked at again")
}
Well to make your life easier you could take a game object.. cube sphere whatever.. attach it to the enemy make it a trigger, and remove its render (making it invisible). then if the player touches this invisible sphere the music is scripted to trigger (one shot).
The only thing is the music will start even if the player doesnt see it just bumps into it's trigger... Otherwise you'll have to do raycasting which is somewhat complex..I know I havent answered your question, but I hope maybe I gave you an idea at least.
Check `This`Out... Here is where you will find out about different things you can do with audio as well as any other variables that can be tweaked in untiy.. Good luck
Doing it this way you would need this script on the trigger object
var Song : AudioClip; function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "player") {
audio.Play(Song);
}
}
Answer by hdgam3r · Aug 26, 2012 at 06:27 AM
Hello Ruffy789,
Have you tried using a Raycast ? You could start a ray from your player position (and with his forward direction) and test if the ray hits any enemy. If so, you can play the sound.
Depends on how precise you want the "sight" to be, but for some cases this approach would work.
Answer by flokkienathur · Aug 26, 2012 at 06:00 AM
Vector3.Dot should do the job for you, read the documentation on that for more info.
Hope this helps!
Answer by Ruffy789 · Aug 26, 2012 at 11:10 AM
Thanks guys, i just noticed that i already made an avoidance system using Raycast and didn't come to the idea to use it for this problem. This is what i figured out:
if (dist < 20 && Physics.Raycast (transform.position, fwd, rayLength) &&!audio.isPlaying) {
audio.PlayOneShot(Scare1);
Debug.Log("Working");
}
}
It works but, (Script had bug with audio) I fixed the audio problem by adding !audio.isPlaying. Silly me forgetting syntaxes
Thanks to everyone who helped me.
Answer by Stardog · Jun 08, 2014 at 12:46 PM
Here's the Dot product method. If the dot == 1, then you're looking at it, if it's -1 then it's behind you.
Transform looker, target;
void Update()
{
if (Vector3.Dot(looker.forward, (target.position - looker.position).normalized) < 0.9f)
{
// LOOKER IS NOT FACING TARGET YET
}
else
{
// LOOKER IS FACING THE TARGET
}
}
Your answer
Follow this Question
Related Questions
Enemy raycast chase player 0 Answers
When the enemy character shoots, the bullet won't go to the position of my player! 2 Answers
How to make my player not lose health when attacking the enemy? 2 Answers
How to do something when an object is in the player sights. 2 Answers
Collision issue with player and enemy 0 Answers