im trying to make a blow gun that shoots darts. my dart spawns at where i want it to spawn but my dart is pointing down instead of pointing it forward. (im trying to point the dart towards the mouse)
local function sendDartToBlowgun(dart:Model, blowgunEndCFrame)
local mousePos = mouse.Hit.Position
local dartPivot = dart:GetPivot()
local dartPrimaryPart = dart.PrimaryPart
local dartPos = blowgunEndCFrame
local rotation = CFrame.Angles(0,math.rad(90),0)
--local direction = (mousePos - dartPos).Unit -- gets direction from dart to mouse, direction pointing at mouse
local newCFrame = CFrame.lookAt(dartPos.Position, mousePos)
dart:PivotTo(newCFrame * rotation)
end
when i click to shoot the dart, it points down:
i tried to use this solution to help me but no luck rip
local function sendDartToBlowgun(dart:Model, blowgunEndCFrame)
local mousePos = mouse.Hit.Position
local newcf = CFrame.lookAt(blowgunEndCFrame.p,mousepos)
dart:PivotTo(newcf)
end
cheers for the help mate. i got my dart to point forward but i can’t really get it to point the dart towards the mouse
local function sendDartToBlowgun(dart:Model, blowgunEndCFrame)
local mousePos = mouse.Hit.Position
local rotation = CFrame.Angles(math.rad(90), 0, 0)
local newCFrame = dart:PivotTo(blowgunEndCFrame * rotation)
end
To point to the mouse position, the code above looks correct although I suspect that the mouse position variable may be wrong.
I can’t be fully sure unfortunately because I don’t know how the mousePos variable is found in your code above, but I think to get an accurate mouse position where the camera is in the world, you should use camera:ViewPortPointToRay() to convert the screen mouse position to the world mouse position. It would look something like this:
local mouseScreenPosition = game:GetService("UserInputService"):GetMouseLocation()
local camera = workspace.CurrentCamera
local mousePos = camera:ViewportPointToRay(mouseScreenPosition.X, mouseScreenPosition.Y, 0)
Then you can use mousePos like you have already done here ^.
Regarding this, the reason the dart is in that position is probably because that is where your dart model that you cloned is initially positioned (basically you did not change where the dart is at all). That is why the PivotTo() part of the code is important. Esentially, when you are setting CFrames of models, you need to use PivotTo() to set the CFrame to change the position and orientation of the dart.
Edit: you can also set CFrames via part.CFrame for BaseParts but PivotTo() is universal for models and baseparts, so I recommend using that.
perfect job mate, it worked!! you were spot on with the mouse position. i’ll look out for that in the future. thank u so much for ur help!!
i just had to change the ray into the vector3 so i could use it for the cframe.look at
local rayEndPoint = mouseRay.Origin + mouseRay.Direction * rayDistance
and i had to put in the * rotation so that the tip of my dart was pointing in the right direction instead of pointing down.
dart:PivotTo(lookat * rotation)
so the full thing was:
local function sendDartToBlowgun(dart:Model, blowgunEndCFrame)
local mouseScreenPosition = game:GetService("UserInputService"):GetMouseLocation()
local camera = workspace.CurrentCamera
local mouseRay = camera:ViewportPointToRay(mouseScreenPosition.X, mouseScreenPosition.Y, 0)
local rayDistance = 500
local rotation = CFrame.Angles(math.rad(90), 0, 0)
local rayEndPoint = mouseRay.Origin + mouseRay.Direction * rayDistance
local lookat = CFrame.lookAt(blowgunEndCFrame.Position, rayEndPoint)
dart:PivotTo(lookat * rotation)
end