Checking Vector3Value.Value Accurately

Hi guys,

I am using Gravity Controller, which changes the player’s gravity so that they are able to walk on the side of walls, terrain, upside down, etc. I believe that is done by changing a Vector3 which I was able to track down via the Script provided in the link above.

This is the script that I am using the track the player’s gravity, and to put that Gravity Value into a Vector3Value object.

Summary
module = require(game:GetService("StarterPlayer").StarterCharacterScripts:WaitForChild("ModuleScript"))

local PLAYERS = module.PLAYERS
local GravityController = module.GravityController
local Controller = module.Controller
local StateTracker = module.StateTracker

local GravityState = script.Parent:WaitForChild("GravityState")

GravityState.Value = Controller.GravityUp


while 1 == 1 do
	GravityState.Value = Controller.GravityUp
	wait()
end

This GIF shows exactly what I mean, and that it works:
https://gyazo.com/9e4d9e2873655edadf7706f3f0579e0b

To the issue that I am experiencing

I have a script that Rotates the Player’s Character whenever the player rotates their camera. It works fine, except when I attempted to walk on a wall, the character would begin glitching out since it was locked to the camera’s rotation.

To counteract this, I made it so that the character would only be locked to the camera’s rotation whenever their Gravity is normal. It works fine, the camera is locked on spawn, and is not locked when walking on a wall, but when returning to Normal Gravity, the character does not lock.

This is because the Gravity's Vector3Value does not return to a static number, and instead does this:
https://gyazo.com/42c3a6d357a956efc5b9316dcb17805b

As you can see in the GIF, is always around the Normal Gravity's Vector3 (0,1,0), but is an exponent very close to these numbers instead of being exactly.

How I believe this can be fixed

I believe that I should be able to check the Magnitude of the Vector3 value, and see if it is within a very close range to the original. However, I am unsure of how I would do this, and this is where I need help.

For Major Clarification:

I basically need help with checking the Magnitude of a Vector3 value, and ensuring that it is within a very small range in order to execute a function.

This is the script I am trying to do this on:

game:GetService("RunService").RenderStepped:Connect(function()
	if plr.Character then
		local rootPart = plr.Character:WaitForChild("HumanoidRootPart")
		if rootPart then
			print(GravityState.Value)
			if GravityState.Value == Normal or GravityState.Value.Magnitude <= .1 then
				rootPart.CFrame = CFrame.new(rootPart.CFrame.p, rootPart.CFrame.p + Vector3.new(cam.CFrame.LookVector.X,0,cam.CFrame.LookVector.Z))
			end
		end
	end
end)

IIf i’m understanding your question correctly and the values in the Vector3 are always expected to be close to integers, then you could round each of them.

So if you currently have your Vector3 in GravityState.Value, you could instead use

local function round(n)
    return math.floor(n + 0.5)
end

RoundedGravityState = Vector3.new(
    round(GravityState.Value.X),
    round(GravityState.Value.Y),
    round(GravityState.Value.Z)
)
1 Like

Thank you for the idea, I didn’t actually know that you could do this.

I just ended up doing a Magnitude Check however;

game:GetService("RunService").RenderStepped:Connect(function()
	if plr.Character then
		local rootPart = plr.Character:WaitForChild("HumanoidRootPart")
		if rootPart then
			
			if (GravityState.Value-Vector3.new(0,1,0)).Magnitude <= .1 then
				rootPart.CFrame = CFrame.new(rootPart.CFrame.p, rootPart.CFrame.p + Vector3.new(cam.CFrame.LookVector.X,0,cam.CFrame.LookVector.Z))
			end
			
		end
	end
end)

This seems to be working fine, but if I end up running into an error down the line with this, and I will definitely use rounding like you replied with.

1 Like