How would I make a part look at another without using CFrame.lookAt()?

Include a standalone, bare-bones rbxl file with only the code you want reviewed.

  • Code Review is for reviewing specific parts of your code, and not your whole game.
  • Code Review is intended for improving already-working code. If you need help debugging your code, please use Scripting Support.

Provide an overview of:

  • What does the code do and what are you not satisfied with?

-Rotates the part along the Y axis so it looks at the player’s position, however the part stays on a “flat” plane, it doesn’t rotate in more than one axis to fully look at the player.

  • What potential improvements have you considered?
    -I’ve tried using Cframe.lookAt(), but it doesn’t work the way I want it to.
  • How (specifically) do you want to improve the code?
    -I want to well, learn how to use the other two axis(if this is the case) so I can make a beam projectile(cylinder) rotate towards the player’s position.

local cPos = v.HumanoidRootPart.Position*Vector3.new(1,0,1)
task.wait(.05)
local aPos = v.HumanoidRootPart.Position*Vector3.new(1,0,1)
local Speed = (aPos-cPos).Magnitude/1.5
local pos = v.Head.Position + Speed*v.Humanoid.WalkSpeed*v.HumanoidRootPart.CFrame.LookVector.Unit
if (pos - script.Parent.Position).Magnitude < magnitude then
	local mainPos = script.Parent.Position
	local dir = (pos-mainPos)
	local flatDir = dir*Vector3.new(1, 0, 1)
	local Pellet = game.ServerStorage.Assets.Ranged["Hyper Shot"]:Clone()
	Pellet.Part1.touched.damage.Value = 20
	Pellet.Part.Size = Vector3.new(flatDir.Magnitude - 1, 1, 1)
	Pellet.Part1.Size = Vector3.new(flatDir.Magnitude, 2.3, 2.3)
	local Y = Vector3.xAxis:Angle(flatDir, Vector3.yAxis)
	Pellet.Part1.CFrame = CFrame.new((pos+script.Parent.Position)/2) * CFrame.Angles(0, Y, 0)
        Pellet.Part.CFrame = CFrame.new((pos+script.Parent.Position)/2) * 
        CFrame.Angles(0, Y, 0)
	Pellet.Parent = workspace
	script.Parent.Sound:Play()
	canShoot = false
        task.wait(.1)
	canShoot = true
end

image

the yellow arrow is how my projectile behaves, it looks at the player (yellow cube) along the Y axis, however it doesnt tilt towards the player.

image

image

Sorry if there are any errors in this post, it’s my first time creating a topic. If there are any questions just ask me!

Can you please clarify what you mean with this a little bit.

It’s possible that you can still use CFrame.LookAt, by adjusting the player position to have one of the values be identical to the part’s position’s value (for either x, y, or z).

Basically, the part has its orientation on only one axis(Y), I want it to rotate in all axis to correctly look to the player’s position and not be floating above the player. Problem is I’m using a cylinder, and it has its front face on the end of its radius

In order to account for the cylinder being rotated, you should make use of the third argument of LookAt, which allows you to specify the ‘up’ direction. Try using Vector3.xAxis and Vector3.zAxis for the argument and see which of them works.

(I believe that you should still be able to use LookAt for your use case, which would be a much simpler solution than trying to custom code)

1 Like

I’ll check this brb

aaaaaaaaaaa

Sadly didn’t work

image

the other axis did just the inverse of what happened here

Try using other direction vectors, such as Vector3.new(1,1,0) and Vector3.new(1,0,1). One of the combinatinos is bound to be correct! (I always end up just doing trial and error until one of the directions looks right.

1 Like

Vector3.new(1,1,1) worked for the block part, however none of the combinations worked for the cylinder part

In some cases it was rotated perpendicularly to the block part rotation


https://gyazo.com/2e0e9c5ed577f4bbb4c429403aaad298

I’ll test around and see if I can figure out what the value should be.

1 Like

Plan B:

workspace.B.CFrame = CFrame.lookAt(workspace.B.Position, workspace.A.Position, Vector3.new(0,1,0)) * CFrame.Angles(0,math.pi/2,0)

Apparently the look vector was doomed to fail. I ran a tool to check every combination and none of them worked D: (including negatives).
I realised I could just try and rotate the result though, which is what the code above does.

1 Like

YAY thanks, very smart of you to rotate it by pi/2 radians, don’t know how I didn’t think about that before, thanks a lot!

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