Hi there, so I’ve been trying to make a Mouse Target Maximum Radius type of system, yet I can’t seem to get anything better than this. So I’ve got the maximum range part when the mouse is hitting a part within the range, but when the mouse is off-range and moving off-range, the model just stays there.
I’m wondering if there’s a function or something to get the furthest CFrame from the character while respecting a maximum radius.
(I don’t know if that was really clear, let me know if it wasn’t and I’ll try making an example of what I’m looking for.)
Here’s the script I’m currently using.
local Players = game:GetService(“Players”)
local RunService = game:GetService(“RunService”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local LocalPlayer = Players.LocalPlayer
-- Waits for character to load
while LocalPlayer.Character == nil do wait() end
local Character = LocalPlayer.Character
-- Getting a scale character
local PositionPreview = ReplicatedStorage:WaitForChild("Other"):WaitForChild("Scale"):Clone()
PositionPreview.Parent = game.Workspace
local Mouse = LocalPlayer:GetMouse()
Mouse.TargetFilter = PositionPreview
if (Character.HumanoidRootPart.Position-Mouse.Hit.p).magnitude <= 20 then
So if any of you have an idea on how to do that, make sure to let me know! Thank you in advance!
Now I can perform a ray cast using workspace:FindPartOnRayWithIgnoreList to achieve the same effects that the player’s mouse object has. Hopefully this is enough information for you to achieve what you want!
I feel that the most feasible way to do this would be to do it with raycasting.
as you have a maximum distance, and an easy way to find the direction, this should be the simplest solution.
local MaxDistance = 20
local origin = Character.HumanoidRootPart.Position
local direction = (Mouse.Hit.Position-origin).Unit
local ray = Ray.new(origin, direction*MaxDistance)
The above would be an example of how it could look.
If you wanted to apply that to your script, I think it could look something like this:
local hrp =Character.HumanoidRootPart
if (hrp.Position-Mouse.Hit.Position).magnitude <= 20 then
local origin = hrp.Position
local ray = Ray.new(origin,(Mouse.Hit.Position-origin).Unit*20)
local p,Pos = workspace:FindPartOnRay(ray, PositionPreview)
Here are some added bits that would be better to use (in my opinion)
Using: local Character = plr.Character or plr.CharacterAdded:Wait() would be prefered to using: while LocalPlayer.Character == nil do wait() end
Using more variables in your script, such as one for Character.HumanoidRootPart would definitely be easier to digest and type than typing Character.HumanoidRootPart everytime you reference it
local minDistance = 0 -- Minimum distance to shoot
local maxDistance = 20 -- Maximum distance to shoot
-- In RenderStepped
local rootPosition = Character.HumanoidRootPart.Position
local difference = rootPosition-Mouse.Hit.p
local distance = math.clamp(difference.Magnitude, minDistance, maxDistance)
local newMouseHit = -(difference.Unit * distance) + rootPosition -- Basically this is making the Magnitude of the difference vector the distance above. Unit vectors have a magnitude of 1, and multiplying a vector by a number multiplies its magnitude, so for example 1 * 20 = 20 magnitude. Then since the difference is rootPosition - mousePosition and we want to replace the mousePosition you can use a little bit of algebra.