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
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.
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.
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.
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.