You see, I recently created a turret system on ROBLOX utilizing CFrame.Angles and ray.direction to move the turret gun, as well as raycasting to shoot the laser beam. It all went well until I found this out.
My issue is that turrets in the origin position, (0,0,0) have no problem for rotating the turret gun, but turrets farther away seem to rotate to the character, only it being relative to the origin. The laser beam is fine, the only problem is the turret gun. I made a YouTube video about this. https://www.youtube.com/watch?v=JqF2AWPeQ4Q&feature=youtu.be
Edit: You have all been requesting the code, here it is.
script.Event.Event:Connect(function(par)
local anim = script.Parent.Parent.Parent.spi.Humanoid:LoadAnimation(script.Animation)
if anim.IsPlaying==false then
anim:Play()
end
script.Parent.Value.Value = true
local ray = Ray.new(script.Parent.Parent.hitty.Position, (par.Position - script.Parent.Parent.hitty.Position).unit * 80)
local beam = Instance.new("Part", script.Parent.Parent)
local distance = (script.Parent.Parent.hitty.Position - par.Position).magnitude
beam.FormFactor = "Custom"
beam.Anchored = true
beam.Locked = true
if script.Parent.Parent.hitty["Laser Beam"].Playing~=true then
script.Parent.Parent.hitty["Laser Beam"]:Play()
end
beam.Transparency = .2
beam.CastShadow = false
beam.CanCollide = false
beam.Material = "Neon"
beam.Color = Color3.fromRGB(106, 119, 232)
beam.Size = Vector3.new(0.2, 0.2, distance)
beam.CFrame = CFrame.new(script.Parent.Parent.hitty.Position, par.Position) * CFrame.new(0, 0, -distance / 2)
local bomx = script.Parent.Position.X
local bomz = script.Parent.Position.Z
script.Parent.CFrame = CFrame.new(script.Parent.Position, ray.Direction)
game:GetService("Debris"):AddItem(beam,.1)
par.Parent.Humanoid.Health = par.Parent.Humanoid.Health - 5
wait(1)
script.Parent.Value.Value = false
script.Parent.Parent.hitty["Laser Beam"]:Stop()
You’re not gonna get much help without posting the actual script. It is much appreciated that you made a video describing and showing exactly what’s wrong, that makes it tremendously easier for us to help you!
Maybe what’s going on is, since ray.Direction is a unitvector which means its length is 1, setting the lookVector of the cframe to that is messing it up, even though lookvector is a unitvector itself cframe doesn’t have a problem with inserting a position of something instead of its direction.
So what you could do is set the lookvector to the position of where the ray landed
script.Parent:SetPrimaryPartCFrame(script.Parent.Positin, position) --assuming that position is where the ray hit because i dont see the part where you use :FindPartOnRay
Ahoi my friend, the solution to your problem is pretty simple!
In this line of code: script.Parent.CFrame = CFrame.new(script.Parent.Position, ray.Direction)
You are creating a CFrame rotated from from the position of your turret to the point in space represented by the direction, not the direction itself. this is why from (0,0,0) it works correctly. The solution is to just add up your direction to the starting position and everything should solve itself
Just replace your original line with this: script.Parent.CFrame = CFrame.new(script.Parent.Position, script.Parent.Position + ray.Direction)
A direction vector is a vector starting from the center of the world and shifting 1 unit towards it’s own direction. This means that a direction vector is in itself a position at the same time. The turrets are pointing towards the point that the direction vector defines, and not the direction itself.
This is why it seems that the turrets looks at the center of the world, they are looking at the center of the world shifted by one unit towards the direction between the turret and the player.