Getting point where Physics.OverlapSphere() is overlapping a collider

Hi,

I am implementing a train game. Here is an example in BabylonJS: http://synth2014.github.io/Age-of-Steam/babylon/rk4.html

I am trying to implement this in Unity 5.3 now.
The locomotives are cuboids and the tracks can only be straight and circular sections connected together.

For proper loco movement on circular sections, I have taken 2 points in the loco - the front-most point and the back-most point. At every iteration I move the front point according to the loco speed and then have to calculate the back point. Since its 3D, and the loco length is fixed, the back of the loco must lie on a sphere centered at the front most point and of radius = length of the loco.

Now I have to intersect the sphere with another section(behind the loco of course) which can be a line or a circle in 3D. Intersecting the line is easy mathematically(without using colliders).

But a intersecting a circular section, is not always stable if I do it via maths: java - Sphere-Sphere intersection and Circle-Sphere intersection - Game Development Stack Exchange

So I am planning to use Physics.OverlapSphere(), with the sphere centered at the front point and radius = length of loco. I will attach colliders to all track sections behind the loco only.

But Physics.OverlapSphere() wont give me the point of intersection between the sphere and the collider. How can I get it?

Is this the correct way: Unity - Scripting API: Collider.ClosestPointOnBounds ?
The above needs the previous track sections to be rigid bodies, and they check for the closest point in their bounding volumes to the given point(loco front point)

Sounds like you would be better off with spherecast or raycast to deliver rich information about the surrounding.

This guy

has a custom character controller that uses overlapspheres and a series of methods for getting the intersection point. You can scroll down to near the bottom of that page to see the start of his journey.

Here is his github
https://github.com/IronWarrior/SuperCharacterController/tree/master/Assets/SuperCharacterController/Core
Have a look at the SuperCollider class and the BSPTree class which is whats used for mesh colliders.

While I dont use his character controller, I have used those 2 classes and I havent had any trouble yet.

1 Like