Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
|
| 05 Aug 2013 11:00 PM |
I am having trouble with my first Raycast. I did dissect and learn the concept from credible free models, but the "bullet" does not move like I ask it nicely to. It just sits at (0, 0, 0). This script runs without errors. Thanks in advance.
startPoint = gun.Handle.CFrame*Vector3.new(-3, -0.6, 0) mag = (mouse.Hit.p - startPoint).magnitude factor = Vector3.new(math.random(-(spread/10)*mag, (spread/10)*mag), math.random(-(spread/10)*mag, (spread/10)*mag), math.random(-(spread/10)*mag, (spread/10)*mag)) fire(mouse.Hit.p + factor)
function raycast(startPoint, endPoint, ign) vector = (endPoint - startPoint) if vector.magnitude < 1000 then hit, position = game.Workspace:FindPartOnRay(Ray.new(startPoint, vector), ign) if hit and position then if hit.Parent.Name == "Gun" or hit.Parent.ClassName=="Hat" then if hit.Parent.Parent ~= nil then if hit.Parent.Parent:findFirstChild("Humanoid")~=nil then return hit.Parent, position end end elseif hit.Transparency == 1 or hit.Name=="Bullet" or hit.Name == "BulletTexture" then vector = (endPoint - position) hit, position = raycast(position, vector, hit) end end return hit, pos end return nil end
function fire(aim) gun.Handle.Fire.Pitch = (math.random(-5,5)/30)+1.25 gun.Handle.Fire:Play() bullet, mesh = Instance.new("Part"), Instance.new("SpecialMesh") bullet.Name, bullet.BrickColor, bullet.Anchored, bullet.CanCollide, bullet.TopSurface, bullet.BottomSurface, bullet.Size = "Bullet", BrickColor.new("Teal"), true, false, 0, 0, Vector3.new(1, 1, 1) mesh.Name, mesh.MeshType, mesh.Scale, mesh.Parent = "BulletMesh", "Brick", Vector3.new(0.15, 0.15, 1), bullet totalDistance = 0 lengthDistance = -rayLength/0.5 startPoint= gun.Handle.CFrame*Vector3.new(-3, -0.6, 0) direction = (aim - startPoint) cframe = CFrame.new(startPoint, direction + startPoint) vector = startPoint + (direction.Unit*999) if (startPoint - vector).magnitude < 1000 then if game.Workspace:FindPartOnRay(Ray.new(game.Players.LocalPlayer.Character.Head.Position, startPoint- game.Players.LocalPlayer.Character.Head.Position), game.Players.LocalPlayer.Character) == nil then hit, position = raycast(startPoint, vector, game.Players.LocalPlayer.Character) bullet.Parent = game.Workspace if hit ~= nil then distance = (startPoint - position).magnitude bullet.CFrame = cframe*CFrame.new(0,0, -distance/2) bullet.Mesh.Scale = Vector3.new(0.15, 0.15, distance) else bullet.CFrame = cframe*CFrame.new(0,0, -rayLength/2) bullet.Mesh.Scale = Vector3.new(0.15,0.15, rayLength) end coroutine.wrap(function() wait(0.5) bullet:destroy() end)() end end end |
|
|
| Report Abuse |
|
|
AeroChase
|
  |
| Joined: 24 May 2013 |
| Total Posts: 82 |
|
| |
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
| |
|
| |
|
|
| 05 Aug 2013 11:37 PM |
| Sorry add print's after every line and see where it stops. |
|
|
| Report Abuse |
|
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
|
| 05 Aug 2013 11:51 PM |
It gets caught at this if then statement:
if game.Workspace:FindPartOnRay(Ray.new(game.Players.LocalPlayer.Character.Head.Position, startPoint - game.Players.LocalPlayer.Character.Head.Position), game.Players.LocalPlayer.Character) == nil then
under the fire function. I don't see anything wrong with it. It should continue, but it doesn't. |
|
|
| Report Abuse |
|
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
|
| 05 Aug 2013 11:56 PM |
| Can anyone tell me what is wrong with that if then statement? |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2013 12:19 AM |
| You are suppose to create the ray before using FindPartOnRay. |
|
|
| Report Abuse |
|
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
|
| 06 Aug 2013 12:25 AM |
| Sorry, I should explain: there are no errors, but it doesn't pass through the if then statement. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2013 12:27 AM |
Here is an example...
chr={game:GetService("Players").LocalPlayer.Character ray = Ray.new(tool.Barrel.CFrame.p,(pos - tool.Barrel.CFrame.p).unit * 999) hit = game.Workspace:FindPartOnRayWithIgnoreList(ray, {chr}) |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2013 12:28 AM |
"Sorry, I should explain: there are no errors, but it doesn't pass through the if then statement."
I understand this. Now I told you what was wrong with the statement try what I said or, continue searching. |
|
|
| Report Abuse |
|
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
|
| 06 Aug 2013 12:46 AM |
| Oh. Sorry. Well, I'll try it. |
|
|
| Report Abuse |
|
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
| |
|
|
| 06 Aug 2013 12:55 AM |
| Hmm, I will search deeper then. |
|
|
| Report Abuse |
|
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
|
| 06 Aug 2013 01:41 AM |
| Big bump. In the meantime, can anyone else help me with my if then statement? Why is it not returning nil? |
|
|
| Report Abuse |
|
|
Xnite515
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 22763 |
|
| |
|
|
| 06 Aug 2013 03:12 AM |
| Whats up with the strange ray direction..? |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2013 03:13 AM |
startPoint - game.Players.LocalPlayer.Character.Head.Position
Are you SURE this is right? |
|
|
| Report Abuse |
|
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
|
| 06 Aug 2013 03:14 AM |
| It's okay. I found The best solution to my problem: forget about it. I'll just remove the if then statement. As much as I'd like to know the answer, the script works as intended without it. |
|
|
| Report Abuse |
|
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
|
| 06 Aug 2013 03:15 AM |
| Well, is that wrong? What should it be? |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2013 03:15 AM |
| But still, what's up with that direction? |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2013 03:16 AM |
| Well where is it supposed to point, and how long is it supposed to be? |
|
|
| Report Abuse |
|
|
Xtreme101
|
  |
| Joined: 03 Jan 2009 |
| Total Posts: 4385 |
|
|
| 06 Aug 2013 03:33 AM |
From the barrel of a gun to the mouse position
Its maximum length is 999, but from the barrel of a gun to the mouse position or anything inbetween (I hope). |
|
|
| Report Abuse |
|
|
| |
|
Xnite515
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 22763 |
|
|
| 06 Aug 2013 12:57 PM |
It works for me? gun = script.Parent mouse = game.Players.LocalPlayer:GetMouse() spread = 0
startPoint = gun.Handle.CFrame*Vector3.new(-3, -0.6, 0) mag = (mouse.Hit.p - startPoint).magnitude factor = Vector3.new(math.random(-(spread/10)*mag, (spread/10)*mag), math.random(-(spread/10)*mag, (spread/10)*mag), math.random(-(spread/10)*mag, (spread/10)*mag))
function raycast(startPoint, endPoint, ign) vector = (endPoint - startPoint) if vector.magnitude < 1000 then hit, position = game.Workspace:FindPartOnRay(Ray.new(startPoint, vector), ign) if hit and position then if hit.Parent.Name == "Gun" or hit.Parent.ClassName=="Hat" then if hit.Parent.Parent ~= nil then if hit.Parent.Parent:findFirstChild("Humanoid")~=nil then return hit.Parent, position end end elseif hit.Transparency == 1 or hit.Name=="Bullet" or hit.Name == "BulletTexture" then vector = (endPoint - position) hit, position = raycast(position, vector, hit) else return hit, position end end return hit, position end end
function fire(aim) rayLength = 999 gun.Handle.Fire.Pitch = (math.random(-5,5)/30)+1.25 gun.Handle.Fire:Play() bullet, mesh = Instance.new("Part"), Instance.new("SpecialMesh") mesh.Name = "Mesh" bullet.Name, bullet.BrickColor, bullet.Anchored, bullet.CanCollide, bullet.TopSurface, bullet.BottomSurface, bullet.Size = "Bullet", BrickColor.new("Teal"), true, false, 0, 0, Vector3.new(1, 1, 1) mesh.Name, mesh.MeshType, mesh.Scale, mesh.Parent = "BulletMesh", "Brick", Vector3.new(0.15, 0.15, 1), bullet totalDistance = 0 lengthDistance = -rayLength/0.5 startPoint= gun.Handle.CFrame*Vector3.new(-3, -0.6, 0) direction = (aim - startPoint) cframe = CFrame.new(startPoint, direction + startPoint) vector = startPoint + (direction.Unit*999) if (startPoint - vector).magnitude < 1000 then if game.Workspace:FindPartOnRay(Ray.new(game.Players.LocalPlayer.Character.Head.Position, startPoint- game.Players.LocalPlayer.Character.Head.Position), game.Players.LocalPlayer.Character) == nil then hit, position = raycast(startPoint, vector, game.Players.LocalPlayer.Character) bullet.Parent = game.Workspace if hit ~= nil then distance = (startPoint - position).magnitude bullet.CFrame = cframe*CFrame.new(0,0, -distance/2) mesh.Scale = Vector3.new(0.15, 0.15, distance) else bullet.CFrame = cframe*CFrame.new(0,0, -rayLength/2) mesh.Scale = Vector3.new(0.15,0.15, rayLength) end wait() bullet:destroy() end end end
fire(mouse.Hit.p + factor) |
|
|
| Report Abuse |
|
|