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 Jerkenhag · Mar 09, 2013 at 10:55 PM · raycastaiattack

Make AI attack on sight, otherwise go to another target.

I have a AI walking towards a moving target at all time which I made work by the code in the else-statement but when I add a raycast to make the AI see the player both the players and the AI's mesh is bugged and wont move nor make the animations that it did before. Here is the code I'm using now

 var target : Transform;
 var player : Transform;
 var source : Transform;
 
 var hit : RaycastHit;
 
 function Update () {
     
     if(collider.Raycast(new Ray(source.transform.position, Vector3.forward), hit, 10.0)){
         
         if(hit.transform.tag == "player"){
     
             GetComponent(NavMeshAgent).destination = player.position;
             
         }
         
     }
     
     else{
     
         GetComponent(NavMeshAgent).destination = target.position;
         
     }
     
 }

which is really small and simple but I don't seem to find what I'm missing and can't see why this affects the mesh rendering.

Comment
Add comment · Show 5
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 Jerkenhag · Mar 09, 2013 at 10:57 PM 0
Share

I tried once again with this code:

 var target : Transform;
 
 function Update () {
     
         GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
     
 }

and that did not work, once again only the light and sound moves around but not the body, same with the player.

Edit: I was able to fix the mesh by replace them but the code still doesn't work. Although it goes straight to the else-statement even if I stand right in front of it.

avatar image Professor Snake · Mar 09, 2013 at 11:49 PM 0
Share

You are casting rays at Vector3.forward which is a global direction and is not affected by the object's rotation. You should try transform.forward ins$$anonymous$$d. Your "player" variable might also be set to null, try adding player=hit.gameObject under the tag check.

avatar image Jerkenhag · Mar 09, 2013 at 11:53 PM 0
Share

I tried this:

 var goal : Transform;
 var player : Transform;
 var source : Transform;
 
 var hit : RaycastHit;
 
 function Update () {
     
     if(Physics.Raycast(source.transform.position, transform.forward, hit, 30.0)){
         
         if(hit.transform.tag == "player"){
     
             GetComponent(Nav$$anonymous$$eshAgent).destination = player.position;
             
         }
         
         else{
         
             GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
         
         }
         
     }
     
     else{
     
         GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
         
     }
     
 }

But it did not work, the AI just walks right through.

Edit: I don't really understand what you mean about adding player=hit.gameObject since I've tried in several different places with no further success.

avatar image ByteSheep · Mar 09, 2013 at 11:55 PM 0
Share

Try replacing the line:

  if(collider.Raycast(new Ray(source.transform.position, Vector3.forward), hit, 10.0)) {

With:

 if (Physics.Raycast (source.transform.position, Vector3.forward, hit, 10.0)) {
avatar image Jerkenhag · Mar 10, 2013 at 12:05 AM 0
Share

As stated in the comment above I tried that and by looking at other scripts I can't see why this wouldn't work.

1 Reply

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

Answer by Khada · Mar 10, 2013 at 01:57 AM

Are you trying to pick towards the player or only directly in front of the AI entity? If you're trying to pick towards the player the ray should be:

 Ray ray = new Ray(transform.position, (player.transform.position - transform.position).normalized);

If you want to pick directly in front of the AI entity, the ray should be:

 Ray ray = new Ray(transform.position, transform.forwards);

Be mindful that 'if(Physics.Raycast)' will return the FIRST collider it hits, so if your AI entity has colliders and you don't use a layer mask when raycasting, you may just be hitting the AI entities' collider.

Also, are you sure 10 far enough for the ray cast and have you checked that the raycast is hitting the player? Use the following line inside a successful raycast to check:

 Debug.Log(hit.collider.tag);
Comment
Add comment · Show 8 · 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 Jerkenhag · Mar 10, 2013 at 12:22 PM 1
Share

Thank you, it worked when I used mask.value and added layer and such. The debug log really helped too, thanks! Although he can look through walls which have a collider. Shouldn't the Physics.Raycast or the layer make so that doesn't happen?

avatar image Khada · Mar 10, 2013 at 12:28 PM 0
Share

You're welcome.

avatar image Jerkenhag · Mar 13, 2013 at 09:35 PM 0
Share

It worked with the tips from above until a couple of hours ago when I applied it to another character.

 var goal : Transform;
 var player : Transform;
 var source : Transform;
 
 var hit : RaycastHit;
 
 var mask : Layer$$anonymous$$ask = 0;
 
 var chasingPlayer = false;
 
 function Update(){
 
     var ray = new Ray(source.transform.position, transform.TransformDirection(transform.forward));
     
     if(chasingPlayer == true){
     
         GetComponent(Nav$$anonymous$$eshAgent).destination = player.position;
         
         GetComponent(Nav$$anonymous$$eshAgent).speed = 7.5;
         
         Debug.Log("Chasing Player");
     
     }
     
     else if(collider.Raycast(ray, hit, 30.0)){
     
         if(hit.collider.tag == "Player"){
         
             GetComponent(Nav$$anonymous$$eshAgent).destination = player.position;
         
             chasingPlayer = true;
             
             Debug.Log(hit.collider.tag);
         
         }
         
         else{
         
             GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
             
             Debug.Log("1");
         
         }
     
     }
     
     else{
     
         GetComponent(Nav$$anonymous$$eshAgent).destination = goal.position;
         
         Debug.Log("2");
     
     }
 
 }

That is the code and I can't figure it out, it seems to be something wrong with the ray since it can move to the goal but doesn't make a notice of the player walking right through it. Also if I set chasingPlayer to true it will chase without any failure. This is the same ray I used before on another character but that one is also broken now. $$anonymous$$y tags are the same and also the layers.

avatar image Khada Jerkenhag · Mar 14, 2013 at 02:29 PM 0
Share

What do you mean a Layer$$anonymous$$ask of -1?

avatar image Khada · Mar 14, 2013 at 12:17 AM 0
Share

As before, you're not using a layer mask and so you are probably detecting the npc's collider. This should be a comment or new question, not an answer.

avatar image Jerkenhag · Mar 14, 2013 at 02:43 PM 0
Share

I saw somewhere that it would work with -1 and it did for some ways but now it doesn't. But I try with 0, 1 and different types and still doesn't work.

Show more comments

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

13 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

Related Questions

Monster starts attack when player enter to sphere collider 1 Answer

AI Field of vision 1 Answer

Using box collider to determine if AI should stop help 1 Answer

How can I draw a ray for direction object is moving? 1 Answer

Finding RayCastHit's Origin Position 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