How to make a raycast pointing in the mouse direction with a certain lengh

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

make a raycast with like 10 studs long pointing in the mouse direction.

  1. What is the issue? Include screenshots / videos if possible!
    i dont know how to do it

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

cry😭

You can get the direction from 2 positions/vectors using Vector2 - Vector1, and then normalizing it using unit: (Vector3.new(end) - Vector3.new(start)).Unit

However, I need to know what the origin is from this, is it pointing to the mouse from the character’s position? or the camera’s?
If it’s the character’s torso, for example, you would do this:

-- Assume every other variable is already set
local mouse = player:GetMouse()
local character = player.Character
local torso = character.Torso

local raycast = workspace:Raycast(torso.Position, (mouse.Hit.Position - torso.Position).Unit * 10)

EDIT: Forgot to add the .Unit

2 Likes

wait i want it to be 10 studs long from the torso (a raycast starting from the torso pointing to the mouse direction) doesnt it need unit?

1 Like

i placed
local raycast = workspace:Raycast(torso.Position, (mouse.Hit.Position - torso.Position).Unit * 10)
and it worked
ty

1 Like

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