CFrame.LookAt() returning wrong value?

Heya!

I’ve been trying to make a fireball attack for one of my enemies and I wanted the fireball to face the target player and then go towards it. The issue is that it’s incredibly inaccurate and doesn’t go towards the player, as seen in the attached image. I figured the issue was most likely with the cframe.lookAt() as the way the fireball moves is using the fireball’s lookVector.

The CFrame.LookAt() is constructed as followed:

if lookAt then
	startCFrame = CFrame.lookAt(origin, lookAt)
end
	
new.PrimaryPart.CFrame = startCFrame 

With “origin” being

enemy.Position + Vector3.new(0,1.8,0)

and “lookAt” being

targetPlayer..Character.PrimaryPart.Position

The movement of the fireball is done using this simple heartbeat loop

connection = runService.Heartbeat:Connect(function(deltaTime)
		new.PrimaryPart.CFrame *= CFrame.new((new.PrimaryPart.CFrame.LookVector) * (velocity.Value * deltaTime))
	end)

Thanks!!!

returns wrong value?
I think the mistake lies somewhere else.
The fireball appears to go in a 90 turn from which you want to send it.
This means you are using the wrong axis to propell the fireball.
Easily fixable but can be overlooked often times.

instead of something likes this
vector3.new(0, 0, 1)
its
vector3.new(1, 0, 0)
or smth like that

Edit:
Maybe also check your operators? (things like *= and stuff)
sometimes its just the math done wrongly.

I just did a test my slamming a decal onto the fireball. It indeed does face the correct direction (the player) but moves incorrectly (not in the front face direction of the fireball for some reason). Any ideas why?

1 Like

not really, send code on how fireball moves please?
(cant fix a non-existant problem on my end)

connection = runService.Heartbeat:Connect(function(deltaTime)
		new.PrimaryPart.CFrame *= CFrame.new((new.PrimaryPart.CFrame.LookVector) * (velocity.Value * deltaTime))
end)

Velocity is literally just a number value

you do realize you are setting its value Times the new amount? *= means itself times the part after

OH- So should I do += instead?

no just “=”, try that
if this fixes the issue then maybe check how to use operators, might prevent this in the future.

Both “=” and “+=” didn’t work. += just threw a bunch of errors and = did nothing

then just do the original *=
it should be that one of the values you used (vector3’s) has the first or the last in the wrong spot since either the X or Z axis has changed.

Try adding or subtracting those with 90? (order X Y Z so first and last)

Actually, I think I fixed it. += did work, I just had to not turn the result into a cframe object. Thanks for the help non the less!!!

1 Like

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