Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

What does the ray:Distance() function do and how do I use it?

Asked by 7 years ago

I am currently making a gun script that uses raycasting. To make sure it does not instantly damage the player, I am making a artificial bullet time script. This requires me getting the distance from the ray origin to the point where the ray hit something, I am currently trying this code in order to make sure the ray damages the player even if the bullet "misses" the player. This is what I have so far:

local character = script.Parent.Parent
    local player = game.Players:GetPlayerFromCharacter(character)
    local mouse = player:GetMouse()

    local ray = Ray.new(muzzle.CFrame.p, (mouse.Hit.p - muzzle.CFrame.p).unit * range)
    local rayDistance = ray:Distance()
    local shotTime = rayDistance / velocity
    local part1 = game.Workspace:FindPartOnRay(ray, character, false, true)

    wait(shotTime)

    local ray2 = Ray.new(muzzle.CFrame.p, (mouse.Hit.p - muzzle.CFrame.p).unit * range)
    local part2 = game.Workspace:FindPartOnRay(ray2, character, false, true)

    if part1 == part2 then
        print("PIE")
    end

I get this error though:

Workspace.M4 Carbine.BulletCreator:14: bad argument #1 to 'Distance' (Vector3 expected, got Ray)

Am I doing the wrong function in order to get what I desired, or am I doing the function?

0
I took a look at the WIKI; apparently, you need to give it a Vector3 value (thus why your error says that). TheeDeathCaster 2368 — 7y
0
To add onto what I said: it's, from what I got from looking at its example, set up to retrieve the distance from Point A to Point B, and from there you can check if the distance is at a certain point. TheeDeathCaster 2368 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

From what I took away from the WIKI, I am guessing this is what it does, although I may be incorrect.

The WIKI doesn't explain well, but from what I looked over & interpreted, it, similar to getting the distance between two objects, returns the closest that Point A and Point B are; here's an example of what I took away:

function returnClosestDistance(Ray, PointB)
    return Ray:Distance(PointB)
    -- You must give it a Vector3 value apparently, thus your error.
end

if returnClosestDistance(ARayHere, PointB) <= 30 then
    -- From what I looked at, it returns the distance between the two points.
    print('Within range!')
else
    print('Out of range!')
end

Note: I didn't explain distances because it seems you know what it is.

Sadly, I can not confirm if this is correct; I'm just going off on what I saw and interpreted, so I'm sorry if the information isn't correct.

Well, I hope this at least helped you or gave you an idea about what it does. :)

In case you're wondering about where I found the information, it's on the WIKI: it's the second code on the top. (Click here to go to the WIKI page, and if you wish to look up the definition, click here to go there, which is also on the same WIKI page.)

I'm sorry that I couldn't help much; but at least I tried. :)

0
A answer is better than no answer. reddarkness321 14 — 7y
0
Yep. :) Did this help at all? TheeDeathCaster 2368 — 7y
0
I have an idea for what to do. I am currently busy with other things though, so I have not tried it out yet. reddarkness321 14 — 7y
0
lol Ok; I'm just glad I helped you out man. :) Good luck! :D TheeDeathCaster 2368 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

I suggest looking at this Wiki page, it should tell you everything! http://wiki.roblox.com/index.php?title=Ray

Answer this question