How do you make a part face the camera?

I need help because im making grass for my game and im wondering if i can make it face the camera if you have any info on this let me know!

2 Likes

You can probably use CurrentCamera.ViewportPointToRay(), then face the part pointing toward the Ray.Origin or something.

1 Like

I dont know how to code that well is there any tutorials or anything?

1 Like

Just to be clear is this what you are trying to do?

1 Like

Yes! Thats exactly what i want to do!

CameraExample.rbxl (26.1 KB)

Okay here’s the file, there’s a single LocalScript in StarterPlayerScripts (it has to be local cuz each player has their own camera).

I don’t know if there’s a better way, but this is what I thought of (maybe someone else will reply with a better way)

Basically every frame (render stepped) I create a ray from the camera with CurrentCamera.ViewportPointToRay(), then I just position the cframe of the part facing that ray.Origin using CFrame.lookAt.

I should warn this is going to move it relative to the center of the part (because that’s the CFrame), if you wanted it to it to have offset or something that would be more work.

I’m guessing in order to make the grass effect it’ll need more work

1 Like

I dont know how you scripters do it but thank you!
This will help alot!

1 Like

No problem, you might need to edit it so it works in your situation, especially if you have a lot of grass parts.

Let me know if you need anything else

1 Like

Is there a way to refrence all f the grass parts in a folder at once?
Im new to scripting so i dont knwo hwo to do stuff like this.

Yes you would have to use a for loop.

CameraExample.rbxl (26.8 KB)

4 Likes

How do i make it only rotate left and right not up and down?

2 Likes

You would do it something like this:

local RS = game:GetService("RunService")

local cam = workspace.CurrentCamera
local part = workspace:WaitForChild("put-ur-part-here") -- replace the text in brackets to the name of your part

local camX = cam.CFrame.Position.X
local camZ = cam.CFrame.Position.Z


RS.RenderStepped:Connect(function()
	part.CFrame = CFrame.lookAt(part.Position, Vector3.new(cam.CFrame.Position.X, 0, cam.CFrame.Position.Z))
end)