Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

why does relative rotation not work with Model:SetPrimaryPartCFrame?

Hello. I am trying to make a tower defense game and i am currently in the stage of making the zombies move. The way i am doing that is through a script that modifies the zombie's rotation and position once it hits a corner part. The problem is, the rotation the script sets is not relative, but it is absolute. Here is my script

local myZombie = script.Parent
print("Myzombie Defined")

while wait(0.3333) do --The server zombies update position thrice per second
	myZombie:SetPrimaryPartCFrame(myZombie.HumanoidRootPart.CFrame*CFrame.new(0,0,-myZombie.ZombieSpeedSPS.Value/3))
	print("Zombie advances")
	
	collidelist = myZombie.HumanoidRootPart:GetTouchingParts() --get parts touching
	for i=1, #collidelist do
		if collidelist[i].Name == "BendWP" then --if part is corner part
			myZombie:SetPrimaryPartCFrame(CFrame.new(collidelist[i].ReferencePart.Position.X, 1.7, collidelist[i].ReferencePart.Position.Z)) -- as the zombie position update is so slow, i adjust its position to correct overshoots and undershoots
			if collidelist[i].IsRightBend.Value == true then --if part mandates to turn right
				local cframe = myZombie.PrimaryPart.CFrame -- Current CFrame
				myZombie:SetPrimaryPartCFrame(cframe * CFrame.new(0, math.rad(90), 0))
		
			else--Else
		
				local cframe = myZombie.PrimaryPart.CFrame -- Current CFrame
				myZombie:SetPrimaryPartCFrame(cframe * CFrame.new(0, math.rad(-90), 0))
			end
		end	
	end
end

PD: It is a model

2 comments

0

I also tried CFrame.Angles and it did not work either

0

Does the zombie have a humanoid? If so, use pathfinding and humanoid:MoveTo(). This will automatically make him turn left / right

1 answer

✓ Accepted answer

Since we know your zombie has a humanoid, we can use pathfinding and humanoid:MoveTo()

Assuming that the script is inside the zombie, we will do:

local humanoid = script.Parent.Humanoid

local pathFindingService = game:GetService("PathfindingService")
local path = pathFindingService:CreatePath() --//Creates a path

local goal = game.Workspace.Goal --//Swap goal with the position you want your zombie to go to

path:ComputeAsync(script.Parent.HumanoidRootPart.Position, goal.Position) --//Analyze the path

local waypoints = path:GetWaypoints()

for k, point in ipairs(waypoints) do
	if path.Status == Enum.PathStatus.Success then
		humanoid:MoveTo(point.Position)

		humanoid.MoveToFinished:Wait()
	end
end

--//All done

I didn't test this since I don't have time too, but let me know if it has any errors.

For information go to: https://www.youtube.com/watch?v=VWKNtqjPKn0

Cheers, Zander

5 comments

0

Ok. i will try it, although i was a bit worried about the resource usage. Thanks

0

It drops no errors, but it does not do anything either. replaced Goal with the part i wanted it to go and placed the script inside the rig model. even created a blank rig to see if it was that

0

Let me see

0

Did you put the script inside of the rig? Also, blank rigs by default have their root anchored

0

oh yeah. let me unanchor it

This discussion is preserved as it was. New answers, comments, and votes are closed.