CFrame.LookAt() acting weird and choosing weird look vectors

Heya!
Let’s cut straight to the chase;
I want some enemies in my tower defense game to be able to let’s say “look” in the direction and idk create a hitbox part into the direction (facing it) where they’re moving on the path. The thing is, enemy positions on the track are stored as Vector3 s. Which makes it impossible for me to determine the look direction.

I’ve found a work around to this problem though. I get the position that is x (very tiny amount, in this case 0.1) studs in from of the enemy on the path and then use CFrame.LookAt() to get the direction the hitbox part has to look.

The thing is that sometimes, more commonly when going around corners, it just summons the hitbox part backwards. So it doesn’t go towards the enemy’s movement direction but in the opposite side.

Here’s my code:

local newPathPos = enemyContructor.paths[enemy.Path]:GetPathPositionInStuds(enemy.T, 0.1)
		
		local hitboxPart = Instance.new("Part")
		hitboxPart.Size = attackRange
		hitboxPart.Anchored = true
		hitboxPart.CanCollide = false
		hitboxPart.CanQuery = false
		hitboxPart.Transparency = 1
		hitboxPart.CFrame = CFrame.lookAt(enemy.Position, newPathPos) * CFrame.new((hitboxPart.CFrame.LookVector * (attackRange.Z/1.75)))
		hitboxPart.Parent = workspace

“attackRange” variable equals to this:

local attackRange = Vector3.new(20,10,100)

I’m certain there’s nothing wrong with my “determine path pos in x studs” code. I’m probably just messing up the cframes. Does anyone know why?

1 Like

Any help would be greatly appreciated!

On this line, you are using hitboxPart.CFrame.LookVector which will be equal to CFrame.new().LookVector as you havent assigned a CFrame to the part yet.

The CFrame on the right side of the “=” is evaluated before being assigned to the part’s property, meaning you can’t use the part’s new CFrame before it is assigned.

Aha. So I want to set the initial cframe first and then manipulate it with the look vector?

I went with this approach but it only made the problem worse…
Bildschirmfoto 2025-07-21 um 10.49.15

What about using += (hitboxPart.CFrame.LookVector * (attackRange.Z/1.75)) instead of *= CFrame.new(....)?

That surprisingly worked!
Thanks!
Although I tried to make a different calculation without the hitbox part and it didn’t work for it.
Or did I mess something up?

local startCFrame = CFrame.lookAt(enemy.Position, newPathPos)
startCFrame += (startCFrame.LookVector * 12) + Vector3.new(0,2.4,0)

What’s the problem with it now?

The same thing as before, aka sometimes placing the thing in the opposite direction

I can’t visualize what you mean, considering I’m trying it from my character’s position to the SpawnLocation and it doesn’t go in the opposite direction. Can you record or make a screenshot?

I managed to just fix it myself. The issue was that “newPathPos” was outdated at the time this piece of code was ran. Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.