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 Jordan104 · Jun 04, 2013 at 02:49 PM · raycastairaycasting

AI Raycast problem

I'm drafting a system where an AI sends out a raycast to a target to check if there is something in front of it and i'm not sure how to go about it. I have a boolean variable called canSeeTarget which if it's set to true the AI draws a green line or if it equals false it draws a red line.

I have another variable which called target which is a transform. So if canSeeTarget equals true, the AI will move towards the target. This may sound a bit unclear so I'll write an example of what I'm doing in javascript

 var target : Transform;
 var canSeeTarget : boolean = false;
 var targetDistance : int;
 var thisAI : GameObject;
 
 function Awake()
 {
    canSeeTarget = false;
 }
 
 function Update()
 {
    targetDistance = Vector3.Distance(target.position,    transform.position);
 
       if(canSeeTarget == true)
     {
       Debug.DrawLine(transform.position, target.position, Color.green);
       thisAI.animation.CrossFade("WalkAim");
     }
    
    if(canSeeTarget == false)
     {
       Debug.DrawLine(transform.position, target.position, Color.red);
       thisAI.animation.CrossFade("Idle");
     }
 }

This script only draws a line to the target and changes colour when canSeeTarget is changed. What I want to do is send a raycast to check if the target can actually be seen instead of seeing through objects and if it has been seen, I can tell it what to do from there. Any suggestions or help would be greatly appreciated

Thanks, Jordz

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 robertbu · Jun 04, 2013 at 02:55 PM

 canSeeTarget = false;
 var hit : RaycastHit;
 if (Physics.Linecast(transform.position, target.position, hit) {
    if (hit.collider.name == "TargtsName") {
         canSeeTarget = true;
     }
 }

Note this only tests to see if the pivot position of the AI can see the pivot position of the target. You may want to raycast from an empty game object at the eyes of the AI. And you may want to test the corners of the bounding box of the target rather than just the pivot.

Comment
Add comment · Show 5 · 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 Jordan104 · Jun 05, 2013 at 03:18 AM 0
Share

I'm getting errors which I don't understand, here how I wrote the code in the script:

    var hit : RaycastHit;
    
    if (Physics.LineCast(transform.position, target.position, hit)) 
    {
        if (hit.collider.tag == "Friendly") 
        {
         canSeeTarget = true;
        }
    }

I have also tried Physics.Raycast and still have errors. What am I doing wrong?

avatar image robertbu · Jun 05, 2013 at 03:29 AM 0
Share

It is Linecast (small 'c'). Sorry about that. Here is code that compiles:

 #pragma strict
 var target : Transform;
 
 function Update() {
 
     var hit : RaycastHit;
     var canSeeTarget = false;
     if (Physics.Linecast(transform.position, target.position, hit)) 
     {
        if (hit.collider.tag == "Friendly") 
        {
         canSeeTarget = true;
        }
     }
 }
avatar image robertbu · Jun 05, 2013 at 03:36 AM 0
Share

Now that I think about it, you should do the other way around if there is a possibility of multiple "Friendly" game objects:

 #pragma strict
 var target : Transform;
  
 function Update() {
  
     var hit : RaycastHit;
     var canSeeTarget = false;
     if (Physics.Linecast(target.position, transform.position, hit)) 
     {
        if (hit.collider.tag == "Player") 
        {
         canSeeTarget = true;
        }
     }
 }

If there were multiple friendly game objects, the original code would give a false positive if another friendly was in the way of the target.

avatar image Jordan104 · Jun 05, 2013 at 06:20 AM 0
Share

Thank you, I will try this

avatar image jordanblythe104 · Jun 05, 2013 at 02:23 PM 0
Share

It worked, thank you

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Finding RayCastHit's Origin Position 2 Answers

Raycasting and enemies ai 2 Answers

Diagonal Raycasting to detect platform returns true, even when false 1 Answer

Enemy AI using raycasting.. **Solved** 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