# RayCast - Detecting a collider anywhere

**URL:** https://discussions.unity.com/t/raycast-detecting-a-collider-anywhere/448848
**Category:** Physics
**Tags:** Scripting
**Created:** [June 29, 2011, 6:53am UTC](https://discussions.unity.com/t/raycast-detecting-a-collider-anywhere/448848 "2011-06-29T06:53:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_89523546](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@anon\_89523546](https://discussions.unity.com/u/anon_89523546)
#### Post date: [June 29, 2011, 6:53am UTC](https://discussions.unity.com/t/raycast-detecting-a-collider-anywhere/448848/1 "2011-06-29T06:53:36Z")

</div>

Hello. I am simulating movements of the world and satellite.

Physics.Raycast(transform.position,transform.forward,hit);

If I understood right, with this code you can find collider just in front of the object. But I must detect any collider any where and by any angle. How can I do it? Can you help me?

---

<div class="post-metadata">

### Author: ![beck](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/beck/32/591219_2.png) [@beck](https://discussions.unity.com/u/beck)
#### Post date: [June 29, 2011, 8:25am UTC](https://discussions.unity.com/t/raycast-detecting-a-collider-anywhere/448848/2 "2011-06-29T08:25:19Z")

</div>

> **[Unity - Scripting API: Physics.SphereCast](https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html)**

---

<div class="post-metadata">

### Author: ![anon\_89523546](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@anon\_89523546](https://discussions.unity.com/u/anon_89523546)
#### Post date: [June 29, 2011, 8:51am UTC](https://discussions.unity.com/t/raycast-detecting-a-collider-anywhere/448848/3 "2011-06-29T08:51:33Z")

</div>

> [@beck](#):
>
> [http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCast.html](http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCast.html)

Thank you you answer. But my problem is with tha direction parameter. →

static function SphereCast (origin : Vector3, radius : float, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool

direction The direction into which to sweep the sphere.

My satellite turns around the world, I added small sphere on the fixed point that rotates with world. When the point (the small sphere) in the satellite field of view it will use line renderer with this point until the satellites don’t see the point.

If I write the direction paramater as “transform.forward or transform.right…” it is exact direction, but the satellite could see the point although they are not in the same direction. I hope I could tell the problem 🙂

---

<div class="post-metadata">

### Author: ![anon\_89523546](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@anon\_89523546](https://discussions.unity.com/u/anon_89523546)
#### Post date: [June 29, 2011, 10:58am UTC](https://discussions.unity.com/t/raycast-detecting-a-collider-anywhere/448848/4 "2011-06-29T10:58:53Z")

</div>

Could anyone help,please?

---

<div class="post-metadata">

### Author: ![spinaljack](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/spinaljack/32/44262_2.png) [@spinaljack](https://discussions.unity.com/u/spinaljack)
#### Post date: [June 29, 2011, 11:22am UTC](https://discussions.unity.com/t/raycast-detecting-a-collider-anywhere/448848/5 "2011-06-29T11:22:35Z")

</div>

You do know raycast directions can be something other than forward, right and up… right?  
To get the direction vector between 2 objects just use this simple formula :

direction = objectB.transform.position - objectA.transform.position;

The raycast function will automatically normalize the vector so just plug it right in

---

<div class="post-metadata">

### Author: ![anon\_89523546](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@anon\_89523546](https://discussions.unity.com/u/anon_89523546)
#### Post date: [June 29, 2011, 12:05pm UTC](https://discussions.unity.com/t/raycast-detecting-a-collider-anywhere/448848/6 "2011-06-29T12:05:20Z")

</div>

Hello, thank you for your answer. But with this : “direction = objectB.transform.position - objectA.transform.position;” there will always be a line between these objects. (like picture2) I need that: when the fixed point in the view of satellite there would be a line between them, otherwise there will not be a line between them. I will explain it with these pictures :

pic1:

pic2:

I mean, the second picture is not working for my project. There would not be a line between them, when the fixed point in the view of satellite (like picture 1) there must be a line between them.

I hope I told the problem clearly, thank you.

---

<div class="post-metadata">

### Author: ![PrimeDerektive](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/primederektive/32/303685_2.png) [@PrimeDerektive](https://discussions.unity.com/u/PrimeDerektive)
#### Post date: [June 29, 2011, 1:27pm UTC](https://discussions.unity.com/t/raycast-detecting-a-collider-anywhere/448848/7 "2011-06-29T13:27:32Z")

</div>

I don’t quite understand what you’re asking…

Are you saying you want to draw lines between the one satellite and all other satellites, but only when the other satellites are within a frontal cone of vision of the satellite, and not obstructed by anything?
