Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

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

Discussion in 'Physics' started by nilcheck, Mar 20, 2016.

  1. nilcheck


    Mar 20, 2016

    I am implementing a train game. Here is an example in BabylonJS:

    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:

    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: ?
    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)
    Last edited: Mar 20, 2016
  2. hippocoder


    Digital Ape

    Apr 11, 2010
    Sounds like you would be better off with spherecast or raycast to deliver rich information about the surrounding.
  3. HiddenMonk


    Dec 19, 2014
    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
    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.
    ouzer likes this.