i need help making this code work with 2D physics.
i found this code that works wonderful with 3d physics, but i am making a 2d game. what i need is for objects to find the closest point on any planet's collider so i can add forces toward that planet. the reason i want it to find the closest point and not the closest planet is because i also want objects to attract to the closest surface. this will allow things such as walking on walls that are attached to a circular object we will call a planet.
here is the code:
i am mainly focused on just finding a Vector2 point to attract the object to. remember, it needs to be 2D
using UnityEngine;
using System.Collections;
//OverlapSphere Test
public class OverlapSphere : MonoBehaviour {
public float maxSearchDistance = 50.0f;
public Collider thisOne;
public LayerMask lm = ~(1<<8 | 1<<9); //Player and Terrain
public Vector3 temp;
void Update ()
{
Collider[] myColArray;
float lastLength = maxSearchDistance;
bool flagSet = false;
myColArray = Physics.OverlapSphere(transform.position, 50.0f, lm);
for(int i = 0; i < myColArray.Length; i++)
{
temp = myColArray[i].ClosestPointOnBounds(transform.position);
Vector3 tempLength = temp - transform.position;
float sqrLength = tempLength.sqrMagnitude;
if(sqrLength < lastLength*lastLength)
{
thisOne = myColArray[i];
lastLength = tempLength.magnitude;
flagSet = true;
}
}
if(flagSet)
{
Debug.Log(thisOne.gameObject.name);
}
}
}
Comment