Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make a Union part rotate to point at another part?

Asked by 5 years ago

The whole model faces screenshots: Front Face | Back Face | Left Face | Right Face |

Here's what this question's focused on: I have a space ship (I made myself) that's supposed to fly at a part representing a planet. The model is made up of 2 Union parts (1 is the body of the ship, the 2nd are the accessories, guns, a window, and an engine at the back of the model) in a model are being moved using CFrame:lerp(x, y, z) and

Here's the problem: The 2 Union part's CFrame don't point at the the same rotation as each other -or what I'm saying is that the body part that is a union and the accessories on the model shift away from each other and don't stay in the same CFrame/Rotation as each other (look at the faces to see what the ship model looks like)- and point to the part (aka "Planet", if you want to be specific) in the Workspace.

Attempted code:

local model = script.Parent
local dest = CFrame.new(72, 13, -27)
local Rotation = Vector3.new(-0.51, 47.21, -0.13)

for i= 0,1,.00001 do --.00001
    wait(0.01) --01
    model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame:lerp(dest, i))
    model.BodyParts.ShipBodyUnion.Orientation = Rotation
    model.BodyParts.Accessories.Orientation = model.BodyParts.ShipBodyUnion.Orientation
    model.BodyParts.Accessories.CFrame:lerp(model.BodyParts.ShipBodyUnion.CFrame, 1)
end

See the Roblox Dev Wiki article for CFrame:lerp()

I'm sorry if this isn't explained enough or it's too word-y, I can clip the less important stuff out or give more details if needed. If you're confused, please tell me what needs to be added/fixed, I know this is a very long question and maybe not so explained well I'm a new developer and I've been trying to resolve this issue for a very long time. So please try to help me here, thank you!

1 answer

Log in to vote
1
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago

If all of the accessories and parts from the ship are within a single model, you should only have to use SetPrimaryPartCFrame once in the loop, as it will move everything within the model relative to the PrimaryPart. If you want the ship to point to a location, you can use the second argument of CFrame.new, which would be a position that the ship "looks" at (the planet). As well as this, when using CFrame:lerp(), you should make sure to keep the starting CFrame constant, otherwise the ship will exponentially increase its speed. So, with all this in mind, you could try something like this:

local model = script.Parent
local dest = Vector3.new(72,13,-27)
local StartCF = CFrame.new(model.PrimaryPart.Position,dest) --point model to destination
local EndCF = StartCF - StartCF.p + dest --keep direction for destination, but use new destination position

for i = 0,1,0.00001 do
    wait(0.01) --the minimum wait time is 0.033, so this may be slightly longer
    model:SetPrimaryPartCFrame(StartCF:lerp(EndCF,i))
end

Ultimately, all this should do is maintain the rotation of the ship across the whole lerp, as the start and end cframes have the same orientation. If your ship still doesn't point the right way, consider adding an invisible PrimaryPart that has its front surface pointing forward.

Hope this helps!

0
Oh my gosh thank you SOOOO much for helping me! You resolved my months of struggles! Thank you thank you thank you!! :DDD Chez_Guy 67 — 5y
Ad

Answer this question