- Home /
How to find the closest point outside a collider from a point inside the collider.
I'm trying to figure out how to reposition a transform that is inside a object with a collider to the nearest position just outside of the collider.
I have been trying to use these 3 methods I've found:
//Method 1
Vector3 closestPoint = avoidanceObject.GetComponent<Collider>().ClosestPoint(target.transform.position);
//Method 2
Vector3 closestPointInBounds = avoidanceObject.GetComponent<Collider>().ClosestPointOnBounds(target.transform.position);
//Method 3
Vector3 pos = target.transform.position;
Quaternion rot = target.transform.rotation;
Vector3 closestPointCollider = Physics.ClosestPoint(target.transform.position, target.GetComponent<Collider>(), pos, rot);
target.transform.position = closestPointCollider;
These three methods do not change the position, is it because the transform is inside the collider? If so how would I find the nearest point or direction to move outside of the collider?
Thank you!
Answer by marck_ozz · Jan 26, 2021 at 05:16 AM
You can try with "Physics.OverlapSphere"
I use a code like this to detect an enemy inside of the radius of a bomb explosion:
public float radius = 200f;
void Explosion()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
foreach(Collider enemy in colliders)
{
if(enemy.CompareTag("Enemy"))
{
EnemyBehaviour exploded = enemy.GetComponent<EnemyBehaviour>();
exploded.DestroyedByBomb();
Destroy(gameObject);
}
}
}
}
and you can Make something like this:
public float radius = 200f;
public GameObject yourObject;
public void DetectNewPosition()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
foreach(Collider newPos in colliders)
{
if(newPos.CompareTag("NearestPoint"))
{
yourObject.transform.Position = newPos.transform.Position;
}
}
}
}
This requires that your "Nearest Point" object have a collider and a tag "NearestPoint"
Another option is to use "OnCollisionEnter" like this:
public GameObject yourObject;
private void OnCollisionEnter(Collision collision1)
{
//Debug.Log("Colision");
if (collision1.collider.tag == "NearestPoint")
{
NewPos();
}
void NewPos()
{
ContactPoint contact = collision1.GetContact(0); //contacts[0];
Quaternion qua = Quaternion.FromToRotation(Vector3.up, contact.normal);
Vector3 pos = contact.point;
yourObject.transform.position = pos;
yourObject.transform.rotation = qua;
}
}
Thank you, I'm not sure I'll be able to integrate it though. I currently use OnTriggerEnter() to generate a list of objects the AI needs to avoid, selecting the nearest one as the target object. I then send out a raycast forward to find the point it hits the object. From there I need to deter$$anonymous$$e the nearest edge so the AI knows what direction to steer. $$anonymous$$aybe I can find the offset of the hitpoint from the center of the object to find a direction to turn.
well, that's another story haha, I suggested that because you mentioned a "Collider".
What about Box 2D colliders? Or Polygon?
Your answer
Follow this Question
Related Questions
physics.OverlapSphere colliders 1 Answer
How do I stack my objects? 1 Answer
How to find the direction and distance to move an object so its no longer jammed into another? 0 Answers
Model Teleports after Animation From Spawn to Idle. Collider doesn't follow 0 Answers
Spawning an object on the opposite side of an activator object with no overlaps 1 Answer