Mouse.Hit.Position isn't accurate

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

my code here:

character rotation:

self.Root.CFrame = self.Root.CFrame:Lerp(CFrame.new(self.Root.Position, Vector3.new(self.Mouse.Hit.Position.X, self.Root.Position.Y, self.Mouse.Hit.Position.Z)),self.LerpSpeed.Value)
	

raycast:

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)
1 Like
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.
1 Like

i got “Unit is not a valid member of CFrame”

What I need is how could I replace the mouse.hit.position because when I point the raycast it goes to infinity

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

It still doesn’t aim correctly, my character looks straight when aiming and the raycast also looks straight


I need the character to point directly towards the mouse and the raycast doesn’t pass by like this, so it works regardless of the distance


Ohhhhhhhhhhh

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.

1 Like

Maybe try using ScreenPointToRay (Camera | Roblox Creator Documentation).

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)
4 Likes

this is more difficult than i thought

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.