Make a Model look at a part

Hello guys, i’m trying to make a House System with models that are already made, like in Adopt me.
But I’m having trouble centering the house models.
There is a centralized part in the middle of the map that serves as a reference, where the models should look. I’ve tried everything but nothing works

  • how do i make my models always look at a fixed position, do i need to set primaryparts or something?
3 Likes

to make a model always face a certain position, you can use the CFrame property of the model and the CFrame.lookAt method. The CFrame.lookAt method takes a position as an argument and returns a CFrame that points towards that position.

Here is an example of how you could use this method to make a model always face a certain position:

local model = game.Workspace.HouseModel

-- The position that the model should face
local targetPosition = Vector3.new(0, 0, 0)

-- Set the model's CFrame to a CFrame that points towards the target position
model.CFrame = CFrame.lookAt(model.Position, targetPosition)

This code sets the CFrame of the HouseModel object to a CFrame that points towards the targetPosition vector. This will make the model always face the targetPosition vector, regardless of where the model is moved.

You can modify this code to suit your needs, such as by using a different position as the target or by using a different object as the model.

4 Likes

i tried, and changed the code to fit my system well, but it’s saying that cframe is not a valid member of model.

2 Likes

maybe do ExteriorModel.NeonDoor.CFrame = CFrame.lookAt(current code in brackets here as u have it)

1 Like

I tested it with other objects and it worked perfectly, thanks in advance, I just need to fix the system.

1 Like
local targetPosition = ...
local model = ...

local cf = model:GetBoundingBox()

local part = Instance.new('Part')
part.CFrame = cf
part.Parent = model

model.PrimaryPart = part
model:PivotTo(CFrame.lookAt(cf.Position,targetPosition))

part:Destroy()
3 Likes

It worked well but…
the model is facing directly in the direction of the part
how do I put only the exact direction?, like the model it’s really fixed to the ground
image

1 Like
model:PivotTo(CFrame.lookAt(cf.Position,Vector3.new(targetPosition.X,cf.Y,targetPosition.Z)))
2 Likes

Fine, thank you!!
another question, here I see that it points exactly to the model, there is the possibility of putting the model pointing only to the exact direction, for example, as the house is on the left side of the model, instead of it pointing directly to the model, it just puts it in an exact direction understood in XYZ
example:

I don’t really get what you mean but you can use CFrame:ToOrientation()

local x,y,z = Part.CFrame:ToOrientation()
-- Part.CFrame is just an example

-- converting them into degrees because they are in radians
x = math.deg(x)
y = math.deg(y)
z = math.deg(z)

print(x,y,z)
1 Like

think of exact directions within a cartesian plane (0,90,180,270)
I want the houses to be in this orientation only, and not their actual position in relation to the targetposition.

Ok, so basically rounding to nearest multiple of a number?

-- the y axis is the left-right rotation

x = math.deg(x)
y = math.round(math.deg(y) / 90) * 90 -- rounds to nearest multiple of 90
z = math.deg(z)

Edit : To use orientation in CFrame,

local half = math.pi / 2

y = math.round(y / half ) * half
-- dont convert to degrees, CFrame.Angles uses radians
local goal = model.PrimaryPart.CFrame * CFrame.Angles(x,y,z)

model:PivotTo(goal)
2 Likes

extaclly, i’ll try that! <3 ty

1 Like

a few changes and done!!
works perfectly, thank you very much indeed, sorry for the lack of clarity in the words.

1 Like

edit: it works fine but it seems that when i change the direction of the plot it keeps looking backwards
image

image
I see that it’s just some problems related to the value of the variables, don’t worry, anyway thank you very much!!

1 Like

y = math.round(math.deg(y) / 90) * 90
is causing my model to be face up

I don’t think that’s what’s causing it to face up. Maybe try 0 for x and z axis?

local goal = model.PrimaryPart.CFrame * CFrame.Angles(0,y,0)
2 Likes

I think the problem is on orientation of the model, because using print() I can see that everything works well.

print(x,y,z)

image

i mean, changing the model orientation in replicated storage, it also changes orientation

1 Like