Want To Make A Rotating Tween While You Are Placing Tower (Tower Defense Game)

hello i have a tower defense game and normally if you want to place a tower you press R key and it rotates it but i want to tween that rotation so it looks more good i am a beginner scripter i did try some code but it didnt work as planned it spinned out of control when i pressed R key

Code when i tried adding tween (i tried from my own mind) :

		elseif input.KeyCode == Enum.KeyCode.R then
			local rotationSpeed = 10
			local rotationTweenInfo = TweenInfo.new(rotationSpeed, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut) 
			rotation += 90
			if rotation == 90 or 180 or -90 or -180 then
				local rotationTween = TweenService:Create(towerToSpawn.PrimaryPart, rotationTweenInfo, {Orientation = Vector3.new(0, rotation, 0)})
				rotationTween:Play()
			end

code when i did not add tween :

		elseif input.KeyCode == Enum.KeyCode.R then
			rotation += 90

i would recommend using cframe

Instead of setting the orientation, try this:

{CFrame *= CFrame.Angles(0,rotation,0)}

You wanna multiply (ie. add) a rotation instead of setting a new one to rotate the tower

When i add the multiplication or addition it underlines the mathematical symbols in red (marks it as invalid)

Edit : tried this code and didn’t work either;

	local TargetCFrame = towerToSpawn.PrimaryPart.CFrame * CFrame.Angles(0, rotation, 0)
				local rotationTween = TweenService:Create(towerToSpawn.PrimaryPart, rotationTweenInfo, {CFrame = TargetCFrame})

I suppose you are using a loop to update the tower’s CFrame, so in this case I would recommend using lerp.

Here’s an example:

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	towerModel.PrimaryPart.CFrame = towerModel.PrimaryPart.CFrame:Lerp(cframe, .1)
end)

And keep the original code:

        elseif input.KeyCode == Enum.KeyCode.R then
			rotation += 90
1 Like

Oh yeah my bad sry, convert rotation to radians since studio uses it a lot.

local TargetCFrame = towerToSpawn.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(rotation), 0)
local rotationTween = TweenService:Create(towerToSpawn.PrimaryPart, rotationTweenInfo, {CFrame = TargetCFrame})

Asumming the issue is the tower still spins uncontrollably

In the targetcframe variable if i remove math.rad or keep math.rad either way it works but without rotating tween the rotation works as normal but just no tween while rotating

		elseif input.KeyCode == Enum.KeyCode.R then
			local rotationSpeed = 0.01
			local rotationTweenInfo = TweenInfo.new(rotationSpeed, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut) 
			rotation += 90
			if rotation == 90 or 180 or -90 or -180 then
				local TargetCFrame = towerToSpawn.PrimaryPart.CFrame * CFrame.Angles(0, rotation, 0)
				local rotationTween = TweenService:Create(towerToSpawn.PrimaryPart, rotationTweenInfo, {CFrame = TargetCFrame})
				rotationTween:Play()
			end

I also setup a few prints to debug this and it printed rotation tween has played maybe it might be the speed or something in tween info

Edit : even after setting rotation to 10 or 1 it’s the same ; the tower rotates without rotation tween

Are you setting the tower CFrame to equal TargetCFrame somewhere else?

No the variable is only in that code

Your rotationspeed in Tween is 0.01 which means the rotation only takes 10 milliseconds. Have you tried changing that?

I have solved this.
I needed to put the tween in render stepped function of my script

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