How To Use This Function?

Hi again,
This is a simple script I found, that checks if the player is looking Up or Down (with their camera). How would I utilize the results of this function? This is more than likely a crazy easy thing, but I cannot for the life of me figure it out. How would I check this function, then do something based on the results?

function UpOrDown(LookVector)
		local LookVector = LookVector.Unit
		local UpVector = Vector3.new(0, 1, 0)
		--Should return a value within range [-1, 1]
		return LookVector:Dot(UpVector)
	end

UpOrDown()
--How to do something based off the results? Ex: if UpOrDown <= 0 then print("Down") end

Thanks for any help!

local function UpOrDown(LookVector)
		local LookVector = LookVector.Unit
		local UpVector = Vector3.new(0, 1, 0)
		--Should return a value within range [-1, 1]
		return LookVector:Dot(UpVector)
	end

local checkUpOrDown = UpOrDown()
--How to do something based off the results? Ex: if UpOrDown <= 0 then print("Down") end

if checkUpOrDown <= 0 then
    print("Down")
else
    print("Up")
end

Maybe this?

1 Like

Thanks for the answer, but I already tried that, and got this error when I tried it again:

Workspace.Infinite_Visions.MoveScript:17: attempt to index nil with ‘Unit’

However, when I do this

local function UpOrDown(LookVector)
		local LookVector = LookVector.Unit
		local UpVector = Vector3.new(0, 1, 0)
		--Should return a value within range [-1, 1]
		return LookVector:Dot(UpVector)
	end

print(UpOrDown)

It works as expected. Do you know how to solve it? Thanks again

You need two Vector3 values to make a useable UnitVector, but a LookVector is already a UnitVector. So I would just remove local LookVector = LookVector.Unit since it’s defining a new variable that is already defined by the function and is just repeating itself.

1 Like

Okay, Thank you for your help! I’m obviously pretty new to scripting, so do you know how I could check the results in an if statement?

As the comment says, Dot Products take two Unit Vectors and return a value in the range of [1,-1]. Since you’re refernecing the LookVector compared to a Unit Vector pointing up you can know if the camera is looking up if the value returned is positive and looking down if it’s negative. Except this same principle is what @Sorbious was discussing.

local function UpOrDown(LookVector)
	local result
	local DotProduct = LookVector:Dot(Vector3.new(0, 1, 0))
	if math.sign(DotProduct) ~= 1 then
		return result = "Down"
	else
		return result = "Up"
	end
end
2 Likes

Thanks for the help, it clarified a lot. Have an amazing day!

1 Like