How do I make a part point at the sun?

I am trying to make the look part point at the sun by setting the lookvector to :GetSunDirection() but its just not rotating it correctly, and it seemingly makes no movement, but when i check the orientation it changes slightly all the time


gT = game.Lighting.ClockTime

gTchange = 0.01

gTwait = 0.1



while true do
	wait (gTwait)
	game.Lighting.ClockTime = game.Lighting.ClockTime + gTchange
	
	print(game.Lighting:GetSunDirection())
	
	local part = workspace.look
	part.Anchored = true
	part.CFrame = CFrame.new(Vector3.new(math.random(0,100),20,math.random(0,100)),game.Lighting:GetSunDirection())
end

Constructing a CFrame with two Vector3s makes a CFrame at the location of the first Vector3 that faces towards the second Vector3. Your second Vector3 from GetSunDirection isn’t a position, it’s a direction. You can see the constructors for CFrames here.

3 Likes

I took the script and placed it in an empty place;

You seem to be using math.random() when setting it’s position which makes it incredibly difficult to see if it’s rotating or not! :stuck_out_tongue:

Okay, so I’ve got it to work.

The issue is that GetSunDirection gets the position Vector from point 0,0,0, as stated on the wiki: image
This means that, with your code, if the part is anywhere that isn’t 0,0,0 it wouldn’t be rotating a noticeable amount.

Here is the code that works, I place the part at (0,0,0), work out the direction and then move it to the desired position. This is a bad way of doing it but it was all I could think of in 2 minutes so if anyone has a better solution feel free to post it.

local lighting = game:GetService("Lighting")

local gT = game.Lighting.ClockTime
local gTchange = 0.01
local gTwait = 0.1

local part = workspace.look
part.Anchored = true

while true do
    lighting.ClockTime = lighting.ClockTime + gTchange

    print(game.Lighting:GetSunDirection())

    -- sozzly's edit
    part.CFrame = CFrame.new(part.Position) * CFrame.new(Vector3.new(), game.Lighting:GetSunDirection())
    --

    wait(gTwait)
end

Side note, it’s better to declare variables outside of your while loop so you don’t have to redefine them on each iteration!

EDIT: probably a better way, as you keep the parts current position

4 Likes

You can multiply the direction to extend it out.

part.CFrame=CFrame.new(Vector3.new(),game:GetService"Lighting":GetSunDirection()*10000)
2 Likes

adding on, to keep the part’s current position, you could do:

part.CFrame = CFrame.new(part.Position) * CFrame.new(Vector3.new(), game.Lighting:GetSunDirection())

4 Likes