Hello, I was wondering, what are rays, because I was using the new AI code in roblox studio, and it showed something like this
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
uis.InputBegan:Connect(function(input)
--player can click on the mouse to shoot
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local ray = workspace.CurrentCamera:ScreenPointToRay(input.Position.X, input.Position.Y)
local part, position = workspace:FindPartOnRay(ray, player.Character)
if part and part.Parent.Name == "Enemy" then
part.Parent.Humanoid.Health -= 5
end
end
end)
Yea that’s possible with rays, albeit in your case I don’t think you’d need a ray. In the example you provided, what it should do is make a ray with the origin being the X and Y of the input position, then it tries to find a part touching that ray, ignoring your character. Probably doesn’t work because the ray is too small to detect anything
For your case of making something spawn in front of you, you don’t need to use any raycasting or anything, you only need to position the object at the player’s HumanoidRootPart’s CFrame multipled with a CFrame offset
object.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -5)
-- CFrame.new(0, 0, -5) would position the object 5 studs in front of the player