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 Fetdressing · Mar 10, 2013 at 12:49 PM · collisionraycastaifollow

AI - Raycast collision and follow player

Hey! I am working on an AI script, trying different things and messing around. I am working on trying to make the enemy chase the player and at the same time prioritize collision. The enemy should chase the player but when encountering an collision move around it and then continue the chase.

         if(GUITest.taBortOk == true && GUITest.pausKnapp == false && GUITest.menuUp == false){
     
     //MOVE AROUND COLLISION
     if(Physics.Raycast(transform.position, transform.forward, hitRay, 25)){
     if(hitRay.transform.tag == "TerrainCollision" || hitRay.transform.tag == "Target" || hitRay.transform.tag == "Moveable" 
         || hitRay.transform.tag == "Wall"){
         //if there is only a turn to the right
         if(Physics.Raycast(transform.position, transform.right, hitRay, 15)){ 
         if(hitRay.transform.tag == null){
         if(Physics.Raycast(transform.position, transform.right * -1, hitRay, 15)){
         if(hitRay.transform.tag != null){
         transform.Rotate(0, Time.deltaTime * 10 + 25, 0); //turn right
         if(chaseIsOn == true){ //when there is nothing in the way anymore - continue chase if chasing
             follow();
         }
         }
         }
         }
         }
         //if there is only a turn to the left
         if(Physics.Raycast(transform.position, transform.right * -1, hitRay, 15)){ 
         if(hitRay.transform.tag == null){
         if(Physics.Raycast(transform.position, transform.right, hitRay, 15)){
         if(hitRay.transform.tag != null){
         transform.Rotate(0, Time.deltaTime * -10 -25, 0); //turn left
         if(chaseIsOn == true){ //when there is nothing in the way anymore - continue chase if chasing
             follow();
             }}}}}
             //if both left and right ways are viable
             if(Physics.Raycast(transform.position, transform.right, hitRay, 15)){ 
             if(hitRay.transform.tag == null){
             if(Physics.Raycast(transform.position, transform.right * -1, hitRay, 15)){
             if(hitRay.transform.tag == null){
             transform.Rotate(0, Time.deltaTime * 10, 0); //turn right slowly
             if(chaseIsOn == true){ //when there is nothing in the way anymore - continue chase if chasing
             follow();
             }}}}}
             
             }}
     //if no way is viable
     if(Physics.Raycast(transform.position, transform.forward, hitRay, 5)){
     if(hitRay.transform.tag == "TerrainCollision" || hitRay.transform.tag == "Target" || hitRay.transform.tag == "Moveable" 
         || hitRay.transform.tag == "Wall"){
         if(Physics.Raycast(transform.position, transform.right, hitRay, 15)){
         if(hitRay.transform.tag != null){
         if(Physics.Raycast(transform.position,transform.right * -1, hitRay, 15)){
         if(hitRay.transform.tag != null){
         transform.Rotate(0, Time.deltaTime * 10 + 25, 0); //turn back the way you came
             if(chaseIsOn == true){ //when there is nothing in the way anymore - continue chase if chasing
             follow();
             }}}}}}}
     }
      


At the position where I put the //MOVE AROUND COLLISION I tried to make the AI(enemy) move the smart way around an object. If the raycast pointing to the left for example is tag == null (no collision), I wan't the enemy to move at to the left if there is an collision forward. If the right and left raycast doesn't return null then he should do the command to just turn transform.Rotate(0,Time.deltaTime * 10 + 25, 0); (right) which will result in a 180 degree turn going back the same way he came.

The follow() function will make the enemy chase the player.

Atm the enemy looses the target when encountering collision and doesn't turn to the raycast that returns null.

Any help or tips would be appreciated, thanks!

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

Answer by sparkzbarca · Mar 11, 2013 at 09:06 PM

looks like you probably have an if error you should AND stuff together to make it easier to read and notice where it is.

this for example which is the test for only turn left

        if(Physics.Raycast(transform.position, transform.right * -1, hitRay, 15)){ 
        if(hitRay.transform.tag == null){
        if(Physics.Raycast(transform.position, transform.right, hitRay, 15)){
        if(hitRay.transform.tag != null){


this is better (not best, that comes in a minute)

 if(physics.raycast(transform.position,-transform.right,15) && !physics.raycast(transform.position,transform.right,15))

 
 that checks only one however really whats best is to remove the long lines

this is best

 //using hard numbers is NEVER a good idea
 //get used to ALWAYS storing the number and going by that
 float CastDistance = 15f;
 
 bool LeftHit;
 bool RightHit;
 Raycasthit LeftHitInfo;
 raycasthit RightHitinfo;
 
 update()
 {
 lefthit = physics.raycast(transform.position,-transform.right,distance,out LeftHitInfo);
 righthit = .....
 
 now just
 
 if(lefthit && !righthit)
 {
 //rotate left
 }

You are constantly recasting the same rays over and over. Store that stuff. Your code is hard to read because raycasts are long lines of code.

if you store and reference you'll get rid of dozens of pointless raycasts and make the code much easier to read and probably notice what you messed up.

Comment
Add comment · Show 2 · 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 Fetdressing · Mar 11, 2013 at 10:21 PM 0
Share

Yea, okay will definitely do that! Does the Raycasthit always take from the raycast above in script? And why is it better to store numbers? Thanks alot for the answer! :)

avatar image Fetdressing · Apr 05, 2013 at 04:12 PM 0
Share

What does the "out" LeftHitInfo do? And when it's !rightHit does that say if the right cast doesn't hit anything at all? Also can I store tags in var? Like hitWall = hitRay.transform.tag == "TerrainCollision"; and then use them in if-statement?

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

11 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

Related Questions

Collider Vision AI question. Solved! 0 Answers

NavMesh AI: follow only if seen 1 Answer

help with AI avoidance script 0 Answers

Monster starts attack when player enter to sphere collider 1 Answer

Wall turret, need help making him a proper prefab that could be scattered around the map. Turret to player, raycast help too. 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