Magnitude for big parts doesn't really work, help with ray cast instead?

  1. What do you want to achieve?
    I want the magnitude to work fine, wherever the player stands (close to the water)

  2. What is the issue?
    The issue is that I don not know whether there is a formula or anything so I can actually have the magnitude working, the script works perfectly fine, just the player distance is a little messed up. E.g if I stand close to the water where the spawn is, the magnitude is different than let’s say 200 studs away, causing the animal(player) to drink even if it’s a little further away from water which ofc is unrealistic.

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
local Event = game.ReplicatedStorage.Events
local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local character = player.Character
local hrp = character.HumanoidRootPart
local water = workspace.Water
local debounce = true

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=11892081628" 

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.E and debounce then
		if Mouse.Target ~= nil then
			if Mouse.Target:FindFirstChild("Drinkable") then
				if character then
					hrp = character:WaitForChild("HumanoidRootPart")
					local Position = hrp.Position

					local magnitude = (water.Position - Position).Magnitude
					print(magnitude)
					if magnitude < 200 then
				debounce = false
				local playAnim = humanoid:LoadAnimation(anim)
				humanoid.WalkSpeed = 0
				playAnim:Play()
				wait(2)
				humanoid.WalkSpeed = 3.4
				debounce = true
				local Object = Mouse.Target
				if Object.Drinkable.Value ~= false and Object.Transparency == 1 then 
					Event.Drink:FireServer(Object, false)
					wait(1)
						end
					end
				end
			end
		end
	end
end)

Therefore, it’s difficult to set a certain magnitude, the red dots show an example value of magnitude.

You’re getting the magnitude of the Water’s position from the root part, and not from where the user is trying to drink from. You could probably use Mouse.Hit.Position although I think it’s better to turn your screen coordinates into a ray then do a raycast, it’ll allow you to have an include exception such as Enum.RaycastFilterType.Include and set it to only the water parts. Although for simplicity sake, just going with the Mouse.Hit.Position as a sustitute of water.Position in local magnitude = (water.Position - Position).Magnitude so that it’s local magnitude = (Mouse.Hit.Position - Position).Magnitude should work good enough for your usecase.


This is only a recommendation.

local screen_coord = UserInputService:GetMouseLocation()
local ray = workspace.CurrentCamera:ScreenPointToRay(screen_coord.X, screen_coord.Y, 1) -- // doesn't need a large distance

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {water}
raycast.FilterType = Enum.RaycastFilterType.Include

local result = workspace:Raycast(ray.Origin, ray.Direction * (max_drinking_distance), raycastParams)

Only reason I recommend doing it this way is it allows more control over the raycast.

2 Likes

If the water you’re using is a base part and not terrain, you can use workspace:GetPartBoundsInRadius()

Thank you so much for your help, it works with both!

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