Make CFrame look towards a part

I’m spawning vehicles, and I want them to look specifically towards the spawner. There will be multiple orientations these spawners are placed at, so I need a way to calculate the orientation I want the model to be at. Here’s my current code, but it doesn’t really rotate the part and just moves it. I’m not very good with CFrames, if someone who is could explain this a little more that would help. :slight_smile:

CFrame.new(
	Vector3.new(
		Spawn.Position.X, 
		Spawn.Position.Y + (Size.Y / 2), 
		Spawn.Position.Z
	) 
) * CFrame.Angles(math.rad(-LookVector.X), 0, 0)
1 Like

To make a “part’s position” look at another position that can be simply defined as CFrame.new(Position, LookAtPosition). So that means that using your code above:

CFrame.new(
	Vector3.new(Spawn.Position.X, 
	  Spawn.Position.Y + (Size.Y / 2), 
	 Spawn.Position.Z),
    PartPosition!
)
3 Likes

How would I achieve this while the part still being flat? I don’t really want vehicles spawning like this :joy:.image

4 Likes

Spawns it looking toward the part, and keeps it level.

local lookToPart = workspace.LookAt;

vehicle:SetPrimaryPartCFrame(CFrame.new(
  Vector3.new(Spawn.Position.X, 
	  Spawn.Position.Y + (Size.Y / 2), 
	 Spawn.Position.Z),
  Vector3.new(lookToPart.Position.X, Spawn.Position.Y + (Size.Y / 2), lookToPart.Position.Z)
));
4 Likes

This worked for me, but one more question. The part I was trying to spawn is engulfing the whole spawner and everything. How would I make sure to keep it on or behind the spawn pad?

1 Like

What exactly do you mean by this? You’re spawning a part and it’s covering everything and it shouldn’t me? (Can you send pictures?)

1 Like

image

1 Like

Are you currently spawning a part in place of the vehicle model for testing?

I’m not entirely sure what the problem is here as I don’t know what you’re trying to go for.

1 Like

Yes, I’ve used GetBoundingBox to make a box the entire size of the model. This represents what would happen if a Model was spawned in because the actual model would fling out of the map before I can see it. Here’s my testing code.

local Plane = script["F-15"]
local Spawn = workspace["Spawn"]
local Orientation, Size = Plane:GetBoundingBox()
local CurrentPosition = Plane.PrimaryPart.Position

local Part = Instance.new("Part", workspace)

Part.Anchored = true
Part.CanCollide = false
Part.Size = Size


Part.CFrame = CFrame.new(
	Vector3.new(
		Spawn.Position.X, 
		Spawn.Position.Y + (Size.Y / 2), 
		Spawn.Position.Z
	) 
, Vector3.new(Spawn.Spawn.Position.X, Spawn.Position.Y + (Size.Y / 2), Spawn.Spawn.Position.Z))
Part.Name = "Oof"

Alright, and so what exactly is the problem? When you say engulfing, do you mean it’s not spawning correctly and it’s partly spawning in the ground? or…?

1 Like

The part is spawning inside the spawner, I want it to spawn on the spawn pad (the grey part on the floor named Spawn) not in the way.

1 Like

So, it’s spawning in the ground rather than above it?

1 Like

It’s spawning off the spawn pad, I want it to be behind the spawn pad on the front and back and centered on the left and right.

Could you possibly provide a quick image of what it should look like? i think i understand but i want to be reassured

When a model gets it’s CFrame set, it sets it relative to the center. If you want it to spawn behind it, you’ll need to move the model in the X or Z axis by half the size (like you did on the y)

1 Like

Alright, thanks. :slight_smile:

1 Like

You you get it working fully as you’re intending.

I’m working on it. It’s a plane and they rotate, so I have to set the CFrame again after it loads and does a 90 degree rotation or set a 90 degree rotation so it can just rotate into its position.

1 Like

So, what happens now with placement, and what’s the one you’re going for?

This keeps happening, I need the plane to stay where I rotated it to. bh0mKBlwcgud2OS8soy1lbdZdbQN18gT