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

How do I make a part face towards the direction it's moving in?

Asked by 7 years ago

I'm currently developing a game that includes animals. The animals are supposed to wander around and when I was making a script to do so, I stumbled across the problem of the animal not facing the way I want it to face when I make it move around. Here's my script (for your reference)

local humanoid = script.Parent:WaitForChild('Humanoid')
local pp1 = script.Parent.Parent:WaitForChild('PathingPart1')
local pp2 = script.Parent.Parent:WaitForChild('PathingPart2')
local pp3 = script.Parent.Parent:WaitForChild('PathingPart3')

script.Parent.PrimaryPart = script.Parent:WaitForChild('Head')

---

while humanoid.Health > 0 do
    wait(5)
    script.Parent:SetPrimaryPartCFrame(???) -- Supposed to make the model rotate towards the part it's moving to
    humanoid:MoveTo(pp1.Position) -- Moves the model
    wait(5)
    script.Parent:SetPrimaryPartCFrame(???) -- Same goes for here
    humanoid:MoveTo(pp2.Position) -- Moves the model
    wait(5)
    script.Parent:SetPrimaryPartCFrame(???) -- And here aswell
    humanoid:MoveTo(pp3.Position) -- Moves the model
end

Thank you for taking some to read/answer/comment on this question

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
7 years ago

To look at a position, you put that position as the second argument (rotation) of the CFrame. I am not sure why it works but it does. If you want to base it off of the velocity... velocity is a vector that is an offset from (0,0,0), so if you tried to use it you would always face near the origin. You can instead offset from your position by adding the two.

part.CFrame = CFrame.new(part.Position,part.Position+part.Velocity

This faces toward where you are moving :)

0
Thank you for your lightning-fast response :-) khfong71 10 — 7y
0
Another problem came up. When I used the method you taught me, the model faced the opposite way as it moves. (E.G. When it's moving forwards, it's facing backwards) Is there any fix to this problem? khfong71 10 — 7y
0
This would be because the model was not built to face the lookVector. You could multiply the velocity by -1, or multiply the final cframe by a CFrame.Angles. cabbler 1942 — 7y
Ad

Answer this question