drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 03:40 PM |
It seems that everytime I click the brick that hit is nil. Is there a way to detect the part at the end of the ray?
local Plr = game.Players.LocalPlayer local Mouse = Plr:GetMouse()
Plr.CameraMode = "LockFirstPerson" Mouse.Button1Down:connect(function() local Ray = Ray.new( Plr.Character.Torso.Position, (Mouse.hit.p - Plr.Character.Torso.CFrame.p).unit ) local hit, position = game.Workspace:FindPartOnRay(Ray, Plr.Character) print(hit) if hit.CanBeGrabbed == true then end
end) |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
| |
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 24 Mar 2014 03:55 PM |
| You've given it a unit vector, so it ends just 1 stud after it starts. Multiply the unit vector by how far you want it to travel. |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 04:00 PM |
lolwut? I want it to travel to where you click. |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 24 Mar 2014 04:01 PM |
I can see that.
(Mouse.hit.p - Plr.Character.Torso.CFrame.p).unit * 100) -- would travel 100 studs
|
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 04:05 PM |
Oh, I see. I was planning on it going infinitely and using .magnitude to calculate if the part is close enough. This makes it simpler. |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 24 Mar 2014 04:10 PM |
| Just multiply by math.huge. That should be infinite enough, lol :> |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
| |
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 04:17 PM |
| Now I just need to figure out how to make the part float in front of you while you 'hold' it. |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 24 Mar 2014 04:32 PM |
You could add a BodyForce of 196.2 * the mass on the Y to negate the gravity, and then apply some multiple of the unit vector of your mouse pos to the brick pos.
Is this gonna be like the physgun from GMOD? :D |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 04:51 PM |
I tried something, failed. lol
repeat hit.Position = Vector3.new((Mouse.hit.p.x - Plr.Character.Torso.Position.x).unit, Mouse.Hit.p.y, (Mouse.hit.p.z - Plr.Character.Torso.Position.z).unit*10) print(hit.Position) wait() until false |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 05:02 PM |
| Yes, it'll be like the Physgun from GMod. |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 05:10 PM |
| Maybe I could use raycasting for this too... |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 24 Mar 2014 05:21 PM |
Haven't tested, but this should act like the Physgun, minus the mousewheel functions.
local Plr = game.Players.LocalPlayer local Mouse = Plr:GetMouse() local mouseDown = false
Plr.CameraMode = "LockFirstPerson" Mouse.Button1Down:connect(function() mouseDown = true
local Ray = Ray.new( Plr.Character.Torso.Position, (Mouse.hit.p - Plr.Character.Torso.CFrame.p).unit )
local hit, position = game.Workspace:FindPartOnRay(Ray, Plr.Character) dist_initial = (Mouse.hit.p - position).Magnitude print(hit)
if hit.CanBeGrabbed == true then bf = Instance.new("BodyForce", hit) repeat local brickpos = hit.Position local dist = (brickpos - Plr.Character.Torso.Position).Magnitude local unit = (brickpos - Plr.Character.Torso.Position).unit local vec = unit * (dist_initial/dist) bf.force = Vector3.new(0,196.2*hit:GetMass(),0) + vec wait() until mouseDown == false
end
end)
Mouse.Button1Up:connect(function() bf:Destroy() mouseDown = false end) |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 05:27 PM |
| when you get to the mouse up function bf would be nil. |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 24 Mar 2014 05:33 PM |
| Well i did write it in a forum post :P. Let me go see if i can fix real quick |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 05:34 PM |
I'm trying: if bf then bf:Destroy() |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
| |
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 05:41 PM |
I fixed it with this, the body force is made, just it doesn't change the value ar anything.
local Plr = game.Players.LocalPlayer local Mouse = Plr:GetMouse() local mouseDown = false
Plr.CameraMode = "LockFirstPerson" Mouse.Button1Down:connect(function() mouseDown = true
local Ray = Ray.new( Plr.Character.Torso.Position, (Mouse.hit.p - Plr.Character.Torso.CFrame.p).unit*10 )
local hit, position = game.Workspace:FindPartOnRay(Ray, Plr.Character) dist_initial = (Mouse.hit.p - position).Magnitude print(hit) if hit ~= nil then if hit.CanBeGrabbed.Value == true then bf = Instance.new("BodyForce", hit) repeat local brickpos = hit.Position local dist = (brickpos - Plr.Character.Torso.Position).Magnitude local unit = (brickpos - Plr.Character.Torso.Position).unit local vec = unit * (dist_initial/dist) bf.force = Vector3.new(0,196.2*hit:GetMass(),0) + vec wait() until mouseDown == false bf:Destroy() end end
end)
Mouse.Button1Up:connect(function() mouseDown = false end)
|
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 24 Mar 2014 05:56 PM |
Hi again.
wait(1) local Plr = game.Players.LocalPlayer local Mouse = Plr:GetMouse() local mouseDown = false bf = nil
--Plr.CameraMode = "LockFirstPerson" Mouse.Button1Down:connect(function() mouseDown = true
local Ray = Ray.new( Plr.Character.Torso.Position, (Mouse.hit.p - Plr.Character.Torso.CFrame.p).unit * 200 )
local hit, position = game.Workspace:FindPartOnRay(Ray, Plr.Character) dist_initial = (Plr.Character.Torso.Position - position).Magnitude
if hit ~= nil then bf = Instance.new("BodyPosition", hit) bf.maxForce = Vector3.new(1,1,1) * 1000 * hit:GetMass() repeat local brickpos = hit.Position local dist = (brickpos - Plr.Character.Torso.Position).Magnitude local unit = (Mouse.hit.p-brickpos).unit local targetpos = Plr.Character.Torso.Position + unit * dist_initial bf.position = targetpos wait() until mouseDown == false bf:Destroy() end
end)
Mouse.Button1Up:connect(function() mouseDown = false end)
This works, but needs some tweaks. I mean, the movement is a bit erratic and can send you flying. :P
Anyway, I'm off to bed now, so hope I've been of some assistance :-). |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 24 Mar 2014 06:45 PM |
| Max length for Ray is 1000 studs |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
|
| 24 Mar 2014 06:51 PM |
That worked, thanks. But how would I make it not be practically right on the player, like 1 stud away. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 24 Mar 2014 06:57 PM |
Replace 'local Ray = Ray.new( Plr.Character.Torso.Position,'
With 'local Ray = Ray.new( (Plr.Character.Torso.CFrame * CFrame.new(0, 0, -1)).p '
Assuming you want it 1 stud forward |
|
|
| Report Abuse |
|
|
drahsid5
|
  |
| Joined: 13 May 2011 |
| Total Posts: 3937 |
|
| |
|