The script works solid at times but there’s an occasion where the ball I create isn’t accurate at all… I’m honestly clueless on what I’m doing wrong. I looked up tutorials, and I even looked at other threads to see if it would solve my problem, but I just can’t find what I’m looking for which is a solution to my problem.
local rep = game:GetService("ReplicatedStorage")
local getMouse = rep:WaitForChild("getMouse")
local Player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
getMouse.OnClientInvoke = function()
local mouse = Player:GetMouse()
mouse.TargetFilter = workspace
local MousePos = uis:GetMouseLocation()
local viewPortMouseRay = workspace.Camera:ViewportPointToRay(MousePos.X, MousePos.Y)
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist
local Descendants = {}
Descendants[#Descendants+1] = workspace.Camera
Descendants[#Descendants+1] = workspace.Field1
Descendants[#Descendants+1] = workspace.SoccerBalls
Descendants[#Descendants+1] = workspace.Box
Descendants[#Descendants+1] = workspace.CurrentCamera
for i,v in pairs(game.Players:GetPlayers()) do
if v.Character then
Descendants[#Descendants+1] = v.Character
end
end
Params.FilterDescendantsInstances = Descendants
local aimCast = workspace:Raycast(viewPortMouseRay.Origin, viewPortMouseRay.Direction * 7500, Params)
if aimCast and aimCast.Position then
if aimCast.Instance then
print(aimCast.Instance)
end
return aimCast.Position
else
print'lol'
return mouse.Hit.Position
end
end
I’ve never seen this function when working with ranged weapons.
What do you want your script to do?
If you simply want to cast a ray from the camera to the you can use this helpful bit of code:
Here I have adapted it to workspace:Raycast() and I’ve given an example InputBegan. Workspace’s version of raycast is preferred over instantiating a ray instance.
You will need to adapt this to your system, but the logic stays the same.
(I wrote this code in this textbox so you might need to fix some errors!)
--// Original by Xan_TheDragon
--// Adapted by Xitral
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Camera = workspace.CurrentCamera
local params = RaycastParams.new()
local function RaycastToMouse(pos)
local ray = Camera:ScreenPointToRay(pos.X, pos.Y)
local result = workspace:Raycast(ray.Origin, ray.Direction * 1000, params)
if result then
return result
end
end
UIS.InputBegan:Connect(function(input, gp) -- gp = game processed
--if gp then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local result = RaycastToMouse(input.Position)
if result then
print(result.Instance.." was clicked!")
end
end
end)
If you meant something else, just reply back and I’ll see what I can do to help!
Basically I want the origin of the direction to be the ball, and I want the direction it goes to be the player’s mouse. But I’m unsure of how I can do this, because when I tried it resulted in the ball not going exactly where I want it to. In other words it’s just inaccurate at times, and it bugs me and players of the game.
So your origin (location A) will be the spawned ball, and your destination (location B) will be the mouse position in 3D space (where your player clicks in the world)?
It’s so simple I just want the position to go in the direction of where the mouse is pointed, but there are occasions where the ball doesn’t go where I point my mouse… I’m just so confused man
This might be the issue, it seems like you’re using the position property incorrectly.
With BodyPosition when setting the Position property the object tries to go directly to that position. The Position property IS your destination.
You’re doing a bunch of complex math when really all you need to do is set the position to where your player clicks their mouse. (If you use my code from earlier that would be result.Position)
--// Original by Xan_TheDragon
--// Adapted by Xitral
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Camera = workspace.CurrentCamera
local Positioner = ...
local params = RaycastParams.new()
local function RaycastToMouse(pos)
local ray = Camera:ScreenPointToRay(pos.X, pos.Y)
local result = workspace:Raycast(ray.Origin, ray.Direction * 1000, params)
if result then
return result
end
end
UIS.InputBegan:Connect(function(input, gp) -- gp = game processed
--if gp then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local result = RaycastToMouse(input.Position)
if result then
Positioner.Position = result.Position
end
end
end)
If you would like your ball to have a curve, I recommend reading EgoMoose’s tutorial from a couple of years ago. I used it to make a snowball fighting system with curved projectiles and it worked wonders.
In particular, you would want to read the physics section since you’ll want your soccer ball to interact with physics.
The position I’m setting the soccerball to is scaled by how much power the player has to their kick. It can’t just go all the way to the position, it has to be scaled yknow so I can’t simply just do MouseHit.Position. And as I’ve seen the positioner does work it just bugs out sometimes which I’m unsure of why it does.
If that’s the case, I really do recommend using EgoMoose’s system.
local t = 1;
local mouse = game.Players.LocalPlayer:GetMouse();
local ball = ... --// The name of your ball
mouse.Button1Down:Connect(function()
local c = Vector3.new(0, -game.Workspace.Gravity, 0); -- set x and z to add side curve
local o = ball.Position
-- calculate the v0 needed to reach mouse.Hit.p
local p = ... -- p for power: set power between 0 and 1
local velocity = (mouse.Hit.p - o - p*c*t*t)/t;
-- have the ball travel that path
local c = ball:Clone();
c.Velocity = velocity;
c.CFrame = CFrame.new(x0);
c.CanCollide = true;
c.Parent = game.Workspace;
end)
Above I have adapted the relevant code by EgoMoose for your use.
C: curve
O: origin
P: power
T: time
I don’t want to use EgoMoose’s system, the reason isn’t because I don’t like his code it’s just that the way I’ve scripted my system is my own way. I want it to be this way, because it’s got different Physics than Velocity. I appreciate your help, thank you for trying to help me. If you still got some ideas, please do let me know I’d be happy to try anything at this point. But I will only try it if it will fix my own code I don’t wish to change.
Since nobodies replied I would like to assume there’s not a solution to this so I will just mark this as solved. Thank you all for your time and trying to help me.