Hello, I am making an ots weapon system and when testing the raycast and the rotation of the character towards the mouse position it does not work as planned
the raycast continues for a long time when i point at something and i also feel that the character rotation when i turn towards the mouse points to the bottom and not where it should
local params = RaycastParams.new()
params.FilterDescendantsInstances = {self.Player}
params.FilterType = Enum.RaycastFilterType.Blacklist
local origin = self.RaycastStartPosition
local direction = CFrame.new(self.RaycastStartPosition, self.Mouse.Hit.p).LookVector * 999999
local result = game.Workspace:Raycast(origin, direction, params)
local direction = CFrame.new(self.RaycastStartPosition, self.Mouse.Hit.p).LookVector * 999999
I’m not exactly sure why you are using lookvectors but…
Basic vector math tells us that direction is the Final Position - Inital Position.
So your direction should be:
local direction = CFrame.new(self.Mouse.Hit.p - self.RaycastStartPosition).Unit * 999999 -- 999999 being the range of the raycast
--also .unit is being used to easily change said range, since .unit gives the same direction of the vector, but with a magnitude (length) of 1.
My bad, you can only get units of a Vector3 and not a CFrame (without specifying the position). I’m not sure how I missed that tbh or why I even kept it as a CFrame since the direction of a raycast takes a vector.
What I need is how could I replace the mouse.hit.position because when I point the raycast it goes to infinity
Also, I already explained in my comment that the length of the vector is always 1 when grabbing the unit, but the direction stays the same. So you can just multiply that unit vector to make it however long you want.
This one should work though:
local Range = 100 -- change this to however many studs long you want this
local direction = (self.Mouse.Hit.p - self.RaycastStartPosition).Unit * Range
The problem here (i think) is that you are starting the raycast from the barrel of the gun. So from the gun’s perspective, its a-okay. BUT from where the camera is positioned, it doesn’t really work.
You’ll have to change the start position to take in account for that, either from taking the Camera’s position, or do some other voodoo magic.
local Camera = workspace.CurrentCamera
local ViewportSize = Camera.ViewportSize
local Middle = Vector2.new(ViewportSize.X/2, ViewportSize.Y/2)
local RayPoint = Camera:ScreenPointToRay(Middle.X, Middle.Y)
local Result = workspace:Raycast(RayPoint.Origin, RayPoint.Direction * 9e5)