• 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 /
This question was closed Jul 02, 2014 at 01:33 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
-1
Question by seb-lopez · Jan 31, 2014 at 05:06 PM · aigamespotlighthorror

ai spot you with flashligt on!!!

im trying to make horro game and i have a simple ai script (i like to modified scripts) but i was wondering how can i make my enemy ai spot you when there a spotlight on (flashligt on) near him. do i need to know a special word for the spotlight?

well what i wanted is when the light is on near him he spots me and charge well im gona show you the Ai script that im using.

 var Distance;
 var Target : Transform;
 var lookAtDistance = 25.0;
 var attackRange = 15.0;
 var moveSpeed = 5.0;
 var Damping = 6.0;
  
 function Update ()
 {
 Distance = Vector3.Distance(Target.position, transform.position);
  
 if (Distance < lookAtDistance)
 {
 renderer.material.color = Color.yellow;
 lookAt();
 }
  
 if (Distance > lookAtDistance)
 {
 renderer.material.color = Color.green;
 }
  
 if (Distance < attackRange)
 {
 renderer.material.color = Color.red;
 attack ();
 }
 }
  
 function lookAt ()
 {
 var rotation = Quaternion.LookRotation(Target.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
  
 function attack ()
 {
 transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
 }

well the function look at is the object ( i use a capsul )when near the object its façing you. the function attack juste charge to you. and what i was thinking is to add a new function bellow the attack function. but i have know idea how to make it attack on light on.

Comment
Add comment · Show 3
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 fafase · Jan 31, 2014 at 05:07 PM 1
Share

You just need your ai to check for the light, is it on.

avatar image OP_toss · Jan 31, 2014 at 06:00 PM 0
Share

Does the spotlight have to be facing him? Does the cone of light have to intersect with his model/face? Or is it just proximity-based?

If it's just by proximity, then just use a combination of Vector3.Distance() and Light.enabled to test your light[s].

avatar image seb-lopez · Jan 31, 2014 at 11:44 PM 0
Share

well what i wanted is when the light is on near him he spots me and charge well im gona show you the Ai script that im using.

 var Distance;
 var Target : Transform;
 var lookAtDistance = 25.0;
 var attackRange = 15.0;
 var moveSpeed = 5.0;
 var Damping = 6.0;
 
 function Update ()
 {
     Distance = Vector3.Distance(Target.position, transform.position);
     
     if (Distance < lookAtDistance)
     {
         renderer.material.color = Color.yellow;
         lookAt();
     }
     
     if (Distance > lookAtDistance)
     {
         renderer.material.color = Color.green;
     }
     
     if (Distance < attackRange)
     {
         renderer.material.color = Color.red;
         attack ();
     }
 }
 
 function lookAt ()
 {
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
 
 function attack ()
 {
     transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
 }

well the function look at is the object ( i use a capsul )when near the object its façing you. the function attack juste charge to you. and what i was thinking is to add a new function bellow the attack function. but i have know idea how to make it attack on light on.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Nanobrain · Feb 04, 2014 at 07:07 AM

Create a cone shaped mesh, add a mesh collider to it, turn it into a trigger, give it a script and make use of the onTriggerEnter() event, then add it as a child to your enemy character. Now, when your player enters into the cone trigger object onTriggerEnter() will fire. You can then check if the collider parameter is the player object. If it is then set a variable in your enemy object to indicate that the player is within the enemie's light path. Then check that variable in your enemy script.

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Horror Game AI script recommendation? 1 Answer

Need an enemy script that makes him wander and detect 1 Answer

AI Script Problem, Horror Game 0 Answers

How to reproduce a sound on collision? 1 Answer

Need a Basic AI Script for Unity 3.0 2 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