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

Is it possible to tween certain axises of a part?

Asked by 4 years ago
Edited 4 years ago

Hello, I have a platforming game which involves the W,A,D keys to move it left, right, or up. The movement works just fine, but I want to tween it to remove the jerkiness of the cframe. I want to only tween the x and y axis and not the z, but I keep getting the resulting errors such as; "attempt to index field 'cframe' (a nil value)" and "x cannot be assigned to". Here is the script I am using for the tweening:

local cfgoal = {}
cfgoal.CFrame = CFrame.new()
cfgoal.CFrame.X = newX
cfgoal.CFrame.Y = newY

local tweeninfo = TweenInfo.new(0.15)
local tween = tweenser:Create(workspace.Player, tweeninfo, cfgoal)

player.CFrame = CFrame.new(newX,newY,newZ)
camera.CFrame = CFrame.new(CameraX,CameraY,newZ + 19.25)

print("cur x - ".. cfgoal.CFrame.X)
print("cur y - ".. cfgoal.CFrame.Y)

The newX and newY variables are listed above the code. Is there anyway I could possibly tween the x and y axises, and leave the z axis alone?

Also, the line the error comes from is on line 3 of this script.

1 answer

Log in to vote
1
Answered by 4 years ago

You can't assign to the X/Y/Z properties of Vector3s and CFrames - you have to manipulate an existing one to accomplish what you want.

If you want to set a CFrame somewhere while respecting its current Z coordinate and angle, you can translate it like so: cf - cf.Position + Vector3.new(goalX, goalY, cf.Position.Z)

To tween this, use the TweenService:GetValue function for full control - you'll have to call it every frame while you're tweening (updating the amount of progress based on the time passed) instead of using TweenService's Create function.

0
What do you mean by calling it every frame? Trollapse 89 — 4y
0
I mean using some sort of loop that runs every frame (ex using RunService). Connect to RunService.Heartbeat with code that updates how much progress the tween should have made (based on time elapsed), then call GetValue for both the X and Y coordinates based on that value. When the tween is done, disconnect the connection to RunService. chess123mate 5873 — 4y
0
I am actually using runservice to keep remaking the tween and updating it so it would work perfectly. Trollapse 89 — 4y
0
Is it fine that I am using renderstepped? Trollapse 89 — 4y
View all comments (3 more)
0
Also, when you say call GetValue for x and y, is the alpha where we put the x and y coord? Trollapse 89 — 4y
0
RenderStepped is fine; if used in a LocalScript it can slow things down if you put a lot of code in there (but it runs immediately after Roblox receives user input and immediately before Roblox starts to render a frame). chess123mate 5873 — 4y
0
My thought with GetValue was that you don't create a Tween at all (since you didn't want to change the .Z position of the object), but every frame (ex every time RenderStepped fires) you figure out how far into the tween you are -- this is "alpha", which could equal: (tick() - tweenStartTime)/tweenTotalTime Call GetValue with that value for the X and then the Y coordinates chess123mate 5873 — 4y
Ad

Answer this question