I did my Projectile System using Raycast
, and it’s working fine, but, i wanted to change the orientation of the object bases where the Ray hit.
I tried to use Raycast.Normal
but isn’t working at the way that I wanted to work.
Here’s a visual representation: vv
What is should be doing:
What is actually doing:
Some kind of orientation is being setted
-1.322, -137.297, -0.002
but not the right one.
The module script:
local RunService = game:GetService("RunService")
local Debris = game:GetService("Debris")
local Projectile = {}
local Trowable
function Projectile.new(Gravity, Whitelist, Wind, Visualize, Object)
local newProjectile = {}
newProjectile.Gravity = Gravity
newProjectile.Whitelist = Whitelist
newProjectile.Wind = Wind
newProjectile.Visualize = Visualize
newProjectile.Object = Object
newProjectile.Params = RaycastParams.new()
newProjectile.Params.FilterType = Enum.RaycastFilterType.Whitelist
newProjectile.Params.FilterDescendantsInstances = Whitelist
function newProjectile:Cast(start, dest, force)
local conversion = 100/9.8
local vForce = (dest - start).Unit * force * conversion
local Acceleration = Vector3.new(self.Wind.X, self.Wind.Y - self.Gravity * 9.8, self.Wind.Z) * conversion
local Time = 0
local StartPos = start
local RayResult = nil
local found = false
if self.Object then
Trowable = self.Object:Clone()
Trowable.Anchored = true
Trowable.CanCollide = false
Trowable.Parent = workspace
for _, Motor6D in pairs(Trowable:GetDescendants()) do
if Motor6D:IsA("Motor6D") then
Motor6D:Destroy()
end
end
end
local Connection = RunService.Heartbeat:Connect(function(DeltaTime)
if not found then
Time += DeltaTime
local ProjPos = Vector3.new(
start.X + vForce.X * Time + 0.5 * Acceleration.X * Time * Time, -- equação maluca q determina a posição
start.Y + vForce.Y * Time + 0.5 * Acceleration.Y * Time * Time,
start.Z + vForce.Z * Time + 0.5 * Acceleration.Z * Time * Time
)
RayResult = workspace:Raycast(StartPos, ProjPos - StartPos, self.Params)
StartPos = ProjPos
if self.Visualize == true then -- Se não houver parte, ele vai criar uma linha do raycast
local Part = Instance.new("Part")
Part.Size = Vector3.new(.5,.5,.5)
Part.Position = ProjPos
Part.Anchored = true
Part.CanCollide = false
Part.Material = Enum.Material.Neon
Part.Color = Color3.fromRGB(0, 255, 0)
Part.Shape = "Ball"
Part.Parent = workspace
Debris:AddItem(Part, .5)
end
Trowable.Position = ProjPos
if RayResult then
found = true
Trowable.CFrame = CFrame.new(RayResult.Position, RayResult.Normal)
if self.Visualize == true then -- debug²
local Part = Instance.new("Part")
Part.Size = Vector3.new(3,3,3)
Part.Position = RayResult.Position
Part.Anchored = true
Part.CanCollide = false
Part.Material = Enum.Material.Neon
Part.Color = Color3.fromRGB(255, 0, 4)
Part.Shape = "Ball"
Part.Parent = workspace
Debris:AddItem(Part, .5)
end
end
end
end)
while not found do
wait()
end
Connection:Disconnect()
if RayResult then
print("foi tlg, detectado")
end
end
return newProjectile
end
return Projectile