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?
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. :)
I suggest looking at this Wiki page, it should tell you everything! http://wiki.roblox.com/index.php?title=Ray