How do I make a basic Air-to-Air Radar using RayCasting?

Hello! I made a jet and I want to implement Radar.

To make this straight-forward I’m going to use real-life information to base this summary on.

Radar works by sending out radio waves in many directions and then receiving data from waves that are bounced back. My basic idea of how to replicate this is by using RayCasting, but the problem is, they don’t have a “Width”, and it’s basically shooting lasers out in a couple different directions, hoping that you’re going to hit something (you probably won’t.)

I’ve seen people look for certain objects in the workspace and seeing if they’re within a certain radius, but what if I only want the radar to point in a straight direction? Like, how do I see if the object is within the “Radar Cone”


Thanks,

  • Luke
3 Likes

I would not use raycasts for this and instead use cframes.

If you only want to view things in a certain direction you can do this using CFrame:ToObjectSpace(…) and then using the math.atan2(z, x) to get the angle.

2 Likes

How would I use the angle?

[Character Limit]

1 Like

Following a similar set up to your picture. For a fixed size cone you can define this by a single angle with respect to the look vector of the plane’s CFrame. The lookVector of a CFrame is in the -z direction of that cframe hence why I’ve written the z and x axes as they are.

When you have a target and are deciding whether or not to display it on the radar you can do this:

-- Get these however works for you
local target = ... 
local planeCFrame = ... 

-- Determines the size of the cone
local RADAR_RANGE = 100 -- Studs
local RADAR_ANGLE = 45 -- Degrees
RADAR_ANGLE = math.rad(RADAR_ANGLE)

local relativePosition = planeCFrame:ToObjectSpace(target.Position)
local distance = relativePosition.Magnitude
local angle = math.acos(- relativePosition.Z /  distance)

if angle < RADAR_ANGLE and distance < RADAR_RANGE then
    display()
end

Edit: Improved image / diagram

2 Likes