how to find Object anywhere in the radius of say 10 units

Hi All,
I am very new to unity and trying to build a game.
A Scenario in my game is following:

  1. Object will start moving in right direction (vector3.right)
  2. while moving, it will continue search for any object in the radius of say 10 units. (means a kind of sphere that continue to search in all directions)
  3. as soon as collision happens with say object2, object have to move from its current position and direction to object2’s direction.

I search and found physics.shperecastall may be the starting point. But, i could not find out how to get the direction of moving object and object with which it colloid.

Any help would really be great!! Please point me what and where to start.
Many thanks in advance!!

After using a Physics.OverlapSphere to get an array of colliders in range you can use “collider.transform.forward” to get it’s heading.

See - Transform-forward
and - Physics.OverlapSphere
for example code.

OverlapShpere() finds any objects without a sphere of a particular position. SphereCastAll() is like like a Raycast in that it takes a sphere and “pushes” it through the scene from a specified starting point in a specified direction looking for hits. OvelapSphere() gives back a array of colliders (hitColliders is used below). Once you find the one you want to use you can do something like:


transform.translate(transform.forward * speed * Time.deltaTime);
Or you can so something like:
transform.position = Vector3.Lerp(transform.position, hitColliders_.transform.position, speed * Time.deltaTime);_