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 Cobradabest · Jan 24, 2013 at 10:19 AM · c#aienemy

How do I get my emeny AI to detect if he sees a player, or one of the good AI? (C#)

I'm having a problem with getting my Enemy AI to detect my player, or any of the good AI.

I'm trying to make it so that if the enemy AI sees the player, or one of the good AI, it goes after it.

I can't get it working, though. Even if I stand right in front of the enemy, he doesn't see me, and doesn't move.

Here's my code:

         RaycastHit hit;
             Vector3 directionOfLine = myTransform.TransformDirection(Vector3.forward);
             Debug.DrawRay((myTransform.position + (Vector3.up * 6) + (Vector3.forward * 4)), directionOfLine * range, Color.red);
             if (Physics.Raycast((myTransform.position + (Vector3.up * 6) + (Vector3.forward * 4)), directionOfLine, out hit, range))
             {
                 if (hit.rigidbody.tag == "Player" || hit.rigidbody.tag == "Rossi Comrade")
                 {
                     target = hit.transform;
                     targetInSight = true;
                 }
             }

(The model origin is on their feet, so it's got "Vector3.Up." added to it to put the line origin at the head)

What am I doing wrong?

Comment
Add comment · Show 2
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 ryba · Jan 24, 2013 at 10:56 AM 0
Share

Log inside raycast and tell if you see if this log prints out in console. Dont put log inside "if (hit.rigidbody.tag" but before - to check if raycast hit anything:

      if (Physics.Raycast((myTransform.position + (Vector3.up * 6) + (Vector3.forward * 4)), directionOfLine, out hit, range)) {
       Debug.Log("BOO$$anonymous$$!");
       if (hit.rigidbody.tag == "Player" || hit.rigidbody.tag == "Rossi Comrade") {
           target = hit.transform;
           targetInSight = true;
       }
      }
avatar image Cobradabest · Jan 24, 2013 at 04:11 PM 0
Share

It registers and logs it, but it still does nothing when the raycast hits the player.

3 Replies

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

Answer by Cobradabest · Jan 25, 2013 at 07:49 PM

I got it working, it turns out the player doesn't have a rigidbody, but a Chaacter controlelr, so I changed it to hit.collider, and it worked!

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 jorjdboss · Jan 24, 2013 at 11:00 AM

 // Parent an empty gameobject called eye and
 // place it at the eye of the AI object with the z axis facing forward
 public Gameobject eye;
 public float viewDistance = 10f;
 public RaycastHit hitInfo;
 Physics.Linecast( eye.transform.position, eye.transform.position + (eye.transform.forward * viewDistance), out hitInfo );
 // Test out by making the rigidbody isKinametic = true and manually dragging the AI to front of player/ good AI
 Debug.Log(hit.collider.tag);

Code not tested but hope it 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 ynka · Jan 24, 2013 at 11:36 AM

Are layers configured properly? Can enemy and player see each other? Maybe layer is ignoring raycasts.

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 Spawn Script Spawning Infinite Enemies [Help] 2 Answers

Make player not be seen by AI, when player in foilage and shadows. 1 Answer

Make AI walk around randomly until Player is Seen. (C#) 2 Answers

how can i make the enemy kill more players one after the other chasing them ??? thank you 0 Answers

have friendly AI look at 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