Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ruffy789 · Aug 26, 2012 at 12:51 AM · playerenemyiflookingat

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")
 }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Panik.Studios · Aug 26, 2012 at 04:40 AM 0
Share

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);
 }

}

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image blackpantherxx · Sep 23, 2012 at 02:17 PM 0
Share

c# or javascript?

avatar image Ruffy789 · Sep 23, 2012 at 04:27 PM 0
Share

javascript

avatar image
0

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
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges