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 /
avatar image
0
Question by wbeltran · May 21, 2018 at 04:21 AM · overlapspherehoming

Homing mechanic, help!

So I want my player character to jump, then home towards an enemy if the jump button is pressed again. So far, this is the code I've got, and for now I'm just trying to get the Physics2D.OverlapCircleAll to work. However I'm not able to get the method to recognize a box in my environment which has been placed in the "Enemy" layer. Moreover, if I increase the radius of the OverlapCircleAll then I detect four objects in the returned list, which doesn't make sense to me because I only have one object in my environment that exists in the "Enemy" layer. Can someone help me out here? I'm really new to Unity so I don't know if I'm missing some simple conventions or something.

 if (Input.GetButtonDown ("Jump") && grounded) {
             print ("Jump");
             velocity.y = jumpTakeOffSpeed;
             homingColliderList = Physics2D.OverlapCircleAll (rb2d.position,homingRadius,LayerMask.NameToLayer("Enemy"),-1,1); // Mess around with these parameters later
             print("Length of Collider List " + homingColliderList.Length);
         } else if (Input.GetButtonUp ("Jump")) {
             if (velocity.y > 0) {
                 velocity.y = velocity.y * .5f;
             }
         }
         if (Input.GetButtonDown ("Jump") && !grounded) {
             
             print (homingColliderList.Length);
             if ((int)homingColliderList.Length != 0) {
                 print ("HomingList has objects!");
                 print (homingColliderList [0].gameObject.tag);
 
                 //homingTarget = Physics2D.Raycast (rb2d.position, rb2d.position - homingColliderList[0].attachedRigidbody.position,homingRadius,9,1f,100f);
                 if (homingTarget != null) {
                     if (homingColliderList[0].gameObject.tag == "Enemy") {
                         
 
 
 
                     }
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 Eno-Khaon · May 21, 2018 at 05:52 AM

You need to change the way you're using your LayerMask in Physics2D.OverlapCircleAll().

A LayerMask allows you to choose many layers based on their bits, rather than being limited to using only a single layer at a time.

To demonstrate how this works, consider this: Let's say you want a Raycast to hit objects only on Layers 3 and 6. That would basically look like...

 Layer#:        76543210
                vvvvvvvv
 Value:         01001000
                 |  |
 Value per bit: 1|  |
 written        26  |
 vertically     843 |
                  21|
                   68
                     4
                      2
                       1
 
 Integer value: 72 (64 + 8)

These values are effectively (26 + 23).

So how do you make use of the LayerMask with this in mind? Well, that's the value in a bit shift. You know which bit your Layer is (LayerMask.NameToLayer("Enemy")), so to use it, you need to shift the bits over. This is where the layout I described above comes into play. The larger value comes first and the smallest value comes last, so a bitwise operation using layers 3 and 6 could look like:

 (1 << 3) + (1 << 6)
 
 00000001
     <--- Move left 3
 00001000
 
 00000001
     <--- Move left 6
 01000000

   00001000
 + 01000000
 = 01001000


Or, in the context you'll be using bit shifts specifically:

 Physics2D.OverlapCircleAll (rb2d.position,homingRadius,1 << LayerMask.NameToLayer("Enemy"),-1,1);


That said, it might be wise to save the layer number from "NameToLayer()" as a variable during Start, since that won't really change during gameplay.

Comment
Add comment · 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

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

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

Related Questions

Whats Wrong? 1 Answer

Overlapsphere : Count objects based on variable ? 1 Answer

Check what overlapSphere is colliding with 0 Answers

A way to check for gameobjects within a radius like Physics.OverlapSphere when there's no collider? 1 Answer

Need help on creating this missile script 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