I’m confused, do you not want it to turn if its hit distance is too far? Or in that case, a turning range?
Could you add a Decal to the Mesh (the part/meshpart that you are moving in the code) and see what direction it’s facing?
I decided to try making this myself from scratch and it works. Make sure the part you are moving has a Decal facing Front the way you want. You can delete the Decal after testing this.
RBLX file:
testing this for devforum.rbxl (56.7 KB)
Video:
no no, the damage and raycast parts are alright, i just need to make the turret face the direction the player’s aiming at; the turret’s direction randomly stops turning when the mouse is close to it which I haven’t coded in + the issue that the turret ain’t turning right
I’m unsure how to fix the facing direction of a mesh when it’s already in Roblox Studio. Changing the PivotPoint for me didn’t work. I assume you’d need to fix it in Blender by turning it 90 degrees, or in the Import 3D settings when you first import a mesh:
Someone else feel free to jump in if there’s an easier way to change it.
Your model might just have the wrong orientation when you loaded it onto a mesh part, I think there should be a setting for that before you put it onto a mesh.
@Reditect Yeah that setting.
Alright so I finished rotating the mesh and exported and it works, nice BUTTT the turret still breaks it’s aim/orientation when the crosshair gets close, I either leave that be or try out :ToWorldSpace()
Unfortunately for me, that was not the solution as it has the same problems as before for some reason, only aiming on one side, canceling the aim etc etc, if only the unanchored model didn’t break when i updated the cframe of the turret
local cf = boat.Body.Turret.Part0.CFrame
local rayPosToLocalSpace = cf:PointToObjectSpace(rayPos)
boat.Body.Turret.C1 = CFrame.new(Vector3.new(0,0,0),Vector3.new(rayPosToLocalSpace.X,0,rayPosToLocalSpace.Z))
You need to convert the RayPosition to the local space of the Turret, as C1/C0 are cframes in local space. If above code doesn’t work, try this instead for cf
local cf = boat.Body.Turret.Part0.CFrame * boat.Body.Turret.C0
I also think op’s issue comes from how he calculates the ray and uses it, your solution would be good, although I think it should use Boat.Body as a reference point for :PointToObjectSpace(), to avoid the rotation of the turret from modifying the returned vector
Oh wait nvm, cf is a constant. But it should still be relative to the boat as you want the turret to react to the boat turning while the ray would be “static”
Edit: I did not realize part0 was part of the boat (and not the turret), but yeah, that would be a good reference part to get the ray’s relative position (could be Part1 though that is welded to the boat, we don’t actually know)
I’m assuming Part0 is what ever the Turret is welded to, and Part1 is the Turret, in my solution
You need to consider the weld equality:
part0.CFrame * C0 == part1.CFrame * C1
Which rearranged gives
C1 = part1.CFrame:Inverse() * part0.CFrame * C0
So all you have to do is calculate the world space cframe of your turret and plug it in:
weld.C1 = worldTurretCF:Inverse() * weld.Part0.CFrame * weld.C0
Hi! I think your problem is here:
Your turret looking from World center to raycast position.
As @Baumz said, you can transform raycast position to local space, but also you can look from turret position instead Vector3.(0,0,0)
, and i think this will have a better performance.
Like this:
boat.Body.Turret.C1 = CFrame.new([Turret position],Vector3.new(rayPos.X, 0 ,rayPos.Z)
I go to sleep, so bye guys