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 /
This question was closed Nov 02, 2019 at 06:50 PM by Revifle_Monarch_Studios for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Revifle_Monarch_Studios · Nov 02, 2019 at 02:42 PM · c#raycast2d-physicsoverlapoverlapsphere

Physics.OlverlapSphere to Physics2D.OverlapCircle "Translation"

Hey, thank you for reading through this thread! As you can already take out of the title and the topics, I have a problem with 2D-Physics. I've been using Unity only in 3D so far, and it has been a long time. Now I started my first 2D Project, which is based on something I already did in 3D.

Now, I want to detect if sprites of certain tags are within a radius of another. Unfortunately, I have no idea how to do this using 2D Physics. I used Physics.OverlapSphere in the 3D-counterpart and will probably have to use Physics2D.OverlapCircle in here, but the latter just makes no sense to me. Can someone translate the following code into 2D for me?

 Collider[] enemies = Physics.OverlapSphere(transform.position, 7);
         foreach (Collider hit in enemies)
         {
             if ((hit.tag == "fox") && (hunger < 85))
             {
                 agent.SetDestination(home.position);
                 threat = hit.gameObject;
                 fleeFlag = true;
 
                 agent.speed = 8f;
 
                 foxAIScript = threat.GetComponent<foxAI>();
             }
 
             if ((reproductionUrge >= 70) && (fleeFlag == false) && (hunger < 75) && (hit.tag == "rabbit") && (male == true) && (age >= 3))
             {
                 lovePartner = hit.gameObject;
                 lovePartnerScript = lovePartner.GetComponent<rabbitAI>();
                 if ((lovePartnerScript.female == true) && (lovePartnerScript.age >= 3))
                 {
                     agent.SetDestination(lovePartner.transform.position);
                     makingLove = true;
                 }
             }
         }
Comment
Add comment · Show 3
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 Reedex · Nov 02, 2019 at 03:56 PM 0
Share

Its the same thing , kinda, not sure if navmesh still works in the 2D environment, That might need tweaking, perhaps writing own A* or something (maybe it's already possible). But you just change Physics.OverlapSphere to Physics2D.OverlapCircleAll which will return array of Collider2D just like this did return array of Colliders, then you will loop through Collider2D array as you did in a foreach for "Collider hit in enemies ". Just to point out though, Unity Answers is a place to look for if you want some more plain facts, i presume it's suppose to be something like extended documentation. So there is a chance that your post will be removed with an excuse. You Should move to Unity Forum If you want to talk about unity and have some broader questions. Generally speaking post here questions that could be answered by facts if you know what i mean.

avatar image Revifle_Monarch_Studios Reedex · Nov 02, 2019 at 06:49 PM 0
Share

Okay, thank you! Did not know that there was such a thing as a "Unity Forum", sorry!

avatar image Reedex Revifle_Monarch_Studios · Nov 02, 2019 at 06:56 PM 0
Share

Sure This is the main page https://forum.unity.com but you will probably mostly be here, ( https://forum.unity.com/forums/scripting.12/ ) just create new thread : - )

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Nov 02, 2019 at 04:33 PM

You don't want to use Physics2D.OverlapCircle but Physics2D.OverlapCircleAll which is the 2d equivalent of OverlapSphere. "OverlapCircle" only returns one object while "OverlapCircleAll" returns all overlapping objects.


Though the 2d physics engine has more optimised methods which avoid garbage allocations. So if you need to call this method a lot you probably want to use Physics2D.OverlapCircleNonAlloc instead. It allows you to create your own predefined Collider2D array which you can reuse for each call. The method returns the number of elements that have been written to the array. So you don't iterate through the whole array, only up to the returned count value minus one. Of course since we use a predefined array you can not get back more elements than your array has elements. So you simply choose an array length that is "long enough" for your usecases.


Unity's NavMesh currently only works in 3d. There's currently no 2d navmesh. So you can not simply "translate" your code fragment from 3d to 2d.

Comment
Add comment · Show 1 · 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 Revifle_Monarch_Studios · Nov 02, 2019 at 06:45 PM 0
Share

Okay, thank you! With "translating" I only meant the Physics part, I know that Nav$$anonymous$$esh does not work in 2D. However, thank you again!

Follow this Question

Answers Answers and Comments

682 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 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 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 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 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I can't manage to detect my collisions 0 Answers

convert code C# to java.. 1 Answer

[C#] Raycast and raycasthit, destroy and instantiate gameobjects 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