Why doesn't changing the orientation work?

So, I’m trying to keep the orientation the same but each time I do, it makes that the position. Kinda basic.

Here’s the script:

local cd = workspace.epicbomberpart.ClickDetector
local plane = workspace.planeryz

if cd.MaxActivationDistance == 10 then
	while true do
		plane:SetPrimaryPartCFrame(CFrame.new(plane.PrimaryPart.Position + Vector3.new(0,0,1)))
		plane:SetPrimaryPartCFrame(CFrame.new(plane.PrimaryPart.Orientation + Vector3.new(0,-90,0)))
		wait(.2)
	end
end
3 Likes

Does replacing the current SetPrimaryPartCFrame lines with this work? This code first calculates the desired orientation in radians by first calculating it in degrees and then converting to radians my mulitiplying with math.pi / 180. Then it constructs a CFrame whose orientation should be correct and whose position is (0, 0, 0). Finally, it adds the correct position to the CFrame.

local orientationInRadians = (plane.PrimaryPart.Orientation + Vector3.new(0, -90, 0)) * (math.pi / 180)
plane:SetPrimaryPartCFrame(CFrame.fromOrientation(orientationInRadians.X, orientationInRadians.Y, orientationInRadians.Z) + (plane.PrimaryPart.Position + Vector3.new(0, 0, 1)))

Edit: I edited the code and the explanation.

1 Like

Hi! I am a developer of the game davidsusbus works on. Yes, your code works! But everytime the plane moves, it also rotates. We want the plane to move, and then after given time, rotate and continue moving the way it was given: creating, a loop where the plane flies around. Any way you can assist us in reaching our goal? Thanks!

Did I understand correctly? Is the plane supposed to periodically follow the same route (I mean that the route is a closed curve and after returning to the starting point the plane starts a new tour)?

If the aforementioned is true, is the route supposed to be a square? Based on the 90 degree addition to the orientation I believe it is supposed to be a square. Here’s some code for a square route.

local RunService = game:GetService("RunService")

local plane = workspace.planeryz
local edgeTravelTime = -- straight movement time between rotations
local planeSpeed = 1 / 0.2 -- change if necessary, I set this based on the Vector3.new(0, 0, 1) and wait(.2) in the original code.

local edgeLength = planeSpeed * edgeTravelTime
local startCFrame = plane.PrimaryPart.CFrame
local startTime = os.clock()

while RunService.Heartbeat:Wait() do
    local timeSpent = os.clock() - startTime
    local howManyEdgesTravelledInTotal = timeSpent / edgeTravelTime
    local edgeStartCFrame = startCFrame * CFrame.new(math.floor(howManyEdgesTravelledInTotal % 4 / 2) * edgeLength, 0, math.floor((howManyEdgesTravelledInTotal + 1) % 4 / 2) * edgeLength) * CFrame.fromOrientation(0, math.floor(howManyEdgesTravelledInTotal) * -math.pi / 2, 0)
    local currentPlaneCf = edgeStartCFrame * CFrame.new(0, 0, howManyEdgesTravelledInTotal % 1 * edgeLength)
    plane:SetPrimaryPartCFrame(currentPlaneCf)
end

Whenever something should repeatedly move along the same route, I recommend calculating the new CFrame based on a fixed CFrame (position and orientation) on the route and the time spent. This is what this code does. The fixed CFrame in the case of this code is the initial CFrame. Calculating the new CFrame using the previous CFrame would cause inaccuracies to stack up over time so the route would gradually differ more and more from the original route.

Edit: I realized I did a mistake so I edited the code.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.