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 ThatUnityNoob · May 13, 2012 at 06:06 PM · raycastenemy

Enemy AI's Raycast Ignoring Player

Hi!

I have a script below that makes my enemy AI dodge objects. But the script also makes it so the enemy dodges the player to. This is a problem when my enemy is shooting at my player and keeps dodging it so the bullets never hit him. If anyone could help me fix the script or edit it that would be great. I tried using layers but i'm not so good with those. Thanks in advance!!!

 function Update()
 {
     // the directional vector to the target
     dir = Vector3.forward * Time.deltaTime * 2;
 
     var hit : RaycastHit;
     // check for forward raycast
     if (Physics.Raycast(transform.position, transform.forward, hit, 15)) // 20 is raycast distance
     {
         if (hit.transform != transform)
         {
             if (Physics.Raycast (transform.position, transform.TransformDirection
                 (Vector3.forward), hit, Mathf.Infinity)) {
 
             Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) *
                                                 hit.distance, Color.yellow);
 
             print ("Did Hit");
             } 
             else {
                 Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) *1000, 
                 Color.white);
 
                 print ("Did not Hit");
             }
 
             Debug.DrawLine (transform.position, hit.point, Color.white);
             dir += hit.normal * 2; // 20 is force to repel by
         }
     }
 
     // more raycasts   
     var leftRay = transform.position;
     var rightRay = transform.position;
 
     leftRay.x -= 2;
     rightRay.x += 2;
 
     // check for leftRay raycast
     if (Physics.Raycast(leftRay, transform.forward, hit, 15)) // 20 is raycast distance
     {
         if (hit.transform != transform)
         {
             Debug.DrawLine (leftRay, hit.point, Color.red);
             dir += hit.normal * 2; // 20 is force to repel by
         }
     }
 
     // check for rightRay raycast
     if (Physics.Raycast(rightRay, transform.forward, hit, 15)) // 20 is raycast distance
     {
         if (hit.transform != transform)
         {
             Debug.DrawLine (rightRay, hit.point, Color.green);
             dir += hit.normal * 2; // 20 is force to repel by
         }
     }
     
     // Movement
     var rot = Quaternion.LookRotation (dir);
     transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime);
 }
Comment
Add comment
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

1 Reply

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

Answer by Ravenmore · May 13, 2012 at 07:08 PM

You're only checking if the object hits itself:

 if (hit.transform != transform)

except for that line your code disregards completely the type of object the raycast hits. Hence the enemy AI will dodge everything except itself =)

You need to add a check for the object's tag, and if it's (for example) "player" then skip the dodging behaviour.

Just set the player's tag to "Player" (it's one of the default ones) And then do:

 if (hit.transform != transform && hit.transform.tag != "Player"){
     (dodge code here)
 }

So you have the AI thinking "if it's not me and not the player, I will start dodging" :)

Comment
Add comment · Show 1 · 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 ThatUnityNoob · May 13, 2012 at 10:26 PM 0
Share

O$$anonymous$$ it works!!! Thanks for the responce but one quick question. Does != mean the oppisite of ==? THAN$$anonymous$$S! Also, The enemys shots are always to the left of the player when the dodge script is attached...any ideas? THAN$$anonymous$$S!!!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the Best way to use Raycast for enemy Fire? 2 Answers

How to make enemy check if it doesn`t see player for certain times it changes state 1 Answer

Raycasting help, enemy damage 0 Answers

Debug.log not working with raycast 1 Answer

Enemy line of sight 1 Answer


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