I have a NPC model that you can move around by clicking.
I also have it set to where when you click a tree it goes over to it.
When the NPC goes over to the tree it is not facing the tree.
I need help figuring out how to get a model to rotate or face towards something.
This can be done with some basic knowledge of Model
and Vector3
/CFrame
.
Note: I will only give you an idea, not a script.
Model
So a Model
has a property of name PrimaryPart
which means that the other parts inside of a model
will maintain their orientation and offset perspective to the PrimaryPart
if it moves, so basically, they will follow and rotate according to their perspective of the PrimaryPart
if it's moved/rotated.
Note: The part that you assign as PrimaryPart has to be a descendant of the Model itself!
Model.PrimaryPart = Model.part --specify your primarypart
Vector3/CFrame
You want the model to face the tree and make it not point vertically. We do this by keeping the PrimaryPart's X/Z position the way it was before but change the Y axis according to the tree position.y.
easily done like this:
lookTo = Vector3.new(Model.Part.Position.X, tree.Position.Y, Model.Part.Position.Z) --we store it as a variable so we can use it on the CFrame late on
Then we use the lookTo
variable onto the PrimaryPart's CFrame to change its rotation, according to the tree.
Model.Part.CFrame = CFrame.new(Model.Part.Position, lookTo)