- Home /
The question is answered, right answer was accepted
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;
}
}
}
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.
Okay, thank you! Did not know that there was such a thing as a "Unity Forum", sorry!
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 : - )
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.
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
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