Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Darkkingdom · Aug 25, 2015 at 05:04 PM · c#raycastenemyraycasthit2d

Strange Raycast problem

Hey guys I have problem with my raycasts,

I have a enemy which search his position in the near of the player and then shoot at him. The most of the time it works even with multiple enemys of this type. But sometimes if more than one of these move in the same areal they stuck without no reason. They didn't have touch each other and it happens just with single enemys.

The problem is the raycast, which checks if the possible position is reachable or if a collider is in the way. Then they stuck, this ray says all poistion arent accessabile because there is a collider in the way, even if there is nothing in a mile.

So I printed a Debug.Log with the tag and the instance ID of the collider which blocks the way. Each time the collider has the tag "Enemy" and Instance ID is something which either didn't exist in the whole game or its something like the animator component of one the other enemys o.O? Even if I delete all enemys, expect the one with the error, from the scene (In playmode of course) there is still a enemy which blocks his way :(

So enough words for now, here is my code:

 void SearchForPositions() {
         atkPos1 = new Vector3 (target.transform.position.x, target.transform.position.y - ShotRange, 0);
         atkPos2 = new Vector3 (target.transform.position.x, target.transform.position.y + ShotRange, 0);
         atkPos3 = new Vector3 (target.transform.position.x - ShotRange, target.transform.position.y, 0);
         atkPos4 = new Vector3 (target.transform.position.x + ShotRange, target.transform.position.y, 0);
         
         accessiblePos1 = checkAccessiblePos (transform.position, atkPos1);    
         accessiblePos2 = checkAccessiblePos (transform.position, atkPos2);    
         accessiblePos3 = checkAccessiblePos (transform.position, atkPos3);    
         accessiblePos4 = checkAccessiblePos (transform.position, atkPos4);    
     }
 
     bool checkAccessiblePos(Vector3 startPos, Vector3 endPos) {
         colliderSize = myCollider.size;
         findRay = new Ray2D(transform.position, (endPos - startPos).normalized);
         findRayHit = Physics2D.CircleCast(findRay.origin, colliderSize.x, findRay.direction, Vector2.Distance(startPos,endPos), layerMask);
         if (findRayHit.collider == null) {
             return true;            
         } else {
             return false;
         }
     }




I'm working one this problem for at least 20 hours but I'm stuck like my enemy ^^'

It would be really great if someone could help me out of this :)

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 Taorcb · Aug 26, 2015 at 08:39 AM 1
Share

Are you using a capsule collider as your hitbox? I've had problems where raycasts hit my AI's hitboxes (it was especially problematic with shooting). You may want to mask out your enemies from the raycast, and see if that helps. Plus, if you're looking to see if you can get somewhere, you're hardly going to notice the guy in your way.

avatar image Darkkingdom · Aug 27, 2015 at 01:49 PM 0
Share

Hey Taorcb thanks for the reply^^

At first I use BoxCollider2D for my enemys. Sadly I need the enemy layer in the raycast. Without it they collide against each other the whole time. But for testing I masked them now out.

Ok now it looks like the error didn't appears o.o

I don't get it^^ Is something wrong the way I send the raycast?

Oh and I turned the option "Raycasts start in Collider" in the projectsettings to off, so I don't think they hit themself. Especially because they work perfect then there is just 1 of the enemys.

================ EDIT: Ok now I got something: When they're stuck and I'm disable there own Boxcollider they can move again. So do the ray really hit themself o.o? Btw. another way to unstuck them is to move them a little bit.

0 Replies

· Add your reply
  • Sort: 

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help making a ledge climber. The problem, transform.position always returns to vector( 0, 0) 0 Answers

Tag doesn't work when gameObject is a child of the Main camera? 1 Answer

How to change the position of a Raycast (on Center of the Player 2D)? 0 Answers

Is there any way how to do raycasts like this? 0 Answers

2D Raycast reflection only changes hit.normal 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