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

why does relative rotation not work with Model:SetPrimaryPartCFrame?

Asked by 4 years ago
Edited 4 years ago

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

0
I also tried CFrame.Angles and it did not work either jokeycraft777 5 — 4y
0
Does the zombie have a humanoid? If so, use pathfinding and humanoid:MoveTo(). This will automatically make him turn left / right Cynical_Innovation 595 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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

0
Ok. i will try it, although i was a bit worried about the resource usage. Thanks jokeycraft777 5 — 4y
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 jokeycraft777 5 — 4y
0
Let me see Cynical_Innovation 595 — 4y
0
Did you put the script inside of the rig? Also, blank rigs by default have their root anchored Cynical_Innovation 595 — 4y
0
oh yeah. let me unanchor it jokeycraft777 5 — 4y
Ad

Answer this question