Script bug or ROBLOX bug when using normalized vector

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like the player to controll the ball

  2. What is the issue? Include screenshots / videos if possible!
    When using a normalised vector everything flickers and i am only able to move some times

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Nothing works I’ve tried recoding the entire thing but no luck

Here is the movement script

local PlayerBall:Part

game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
	wait()
	
	workspace.CurrentCamera.CameraSubject = char.HumanoidRootPart
	PlayerBall = workspace.PlayerBalls[game.Players.LocalPlayer.Name .. "'s ball"]
	
end)

local MaxRotoSpeed = 50
local MovementVector

game.Players.LocalPlayer.CharacterAdded:Wait()
wait(.2)

game["Run Service"].Heartbeat:Connect(function(dt)
	
	local BV
	local CheckSpeed = function()
		
		BV = PlayerBall.AssemblyAngularVelocity
		PlayerBall.AssemblyAngularVelocity = Vector3.new(math.clamp(BV.X,-MaxRotoSpeed,MaxRotoSpeed),math.clamp(BV.Y,-MaxRotoSpeed,MaxRotoSpeed),math.clamp(BV.Z,-MaxRotoSpeed,MaxRotoSpeed))
		
	end
	
	MovementVector = Vector3.new(0,0,0)
	
	local Keys = game.UserInputService:GetKeysPressed()
	for i=1, #Keys do
		
		local CurrentKey:InputObject = Keys[i]

		if (CurrentKey.KeyCode == Enum.KeyCode.W) then
			
			if PlayerBall.Anchored then return end
			MovementVector += workspace.CurrentCamera.CFrame.LookVector * Vector3.new(1,0,1)	
			
		elseif (CurrentKey.KeyCode == Enum.KeyCode.S) then
			
			if PlayerBall.Anchored then return end
			MovementVector -= workspace.CurrentCamera.CFrame.LookVector * Vector3.new(1,0,1)
			
		elseif (CurrentKey.KeyCode == Enum.KeyCode.D) then
			
			if PlayerBall.Anchored then return end
			MovementVector += workspace.CurrentCamera.CFrame.RightVector * Vector3.new(1,0,1)
			
		elseif (CurrentKey.KeyCode == Enum.KeyCode.A) then
			
			if PlayerBall.Anchored then return end
			MovementVector -= workspace.CurrentCamera.CFrame.RightVector * Vector3.new(1,0,1)
			
		end
		
	end
	
	--Nomalised
	MovementVector = MovementVector.Unit
	
	local MaxSpeed = 50
	local Speed = math.sqrt( math.pow(PlayerBall.AssemblyLinearVelocity.X,2) + math.pow(PlayerBall.AssemblyLinearVelocity.Z,2) )
	
	local overSpeed = Speed/MaxSpeed
	local clampedVector = Vector3.new(PlayerBall.AssemblyLinearVelocity.X/overSpeed, 0, PlayerBall.AssemblyLinearVelocity.Z/overSpeed)
		
	PlayerBall.AssemblyLinearVelocity += (MovementVector*150 * (dt)) * (1 - clampedVector:Dot(MovementVector) / MaxSpeed)
	
	
end)

Have you tried printing the MovementVector after normalizing?

Something else: normalizing is only defined for some vectors, namely non-zero ones. (0, 0, 0).Magnitude = 0 , so normalizing it involves division by zero which is not defined. If the player is not pressing any keys you’ll get the (NaN, NaN, Nan) vector which uhhh I don’t know what that does. Maybe that’s what breaks things?

Try

function unitOr0(v: Vector3): Vector3
    if v == Vector3.zero then return v end
    return v.Unit
end

or just check if MovementVector is (0, 0, 0) before calling .Unit.

1 Like

Yep, that seemed to be the problem! It was returning a vector3 with nan for all values!

So I’m just checking if it’s nan and if it is I’m just setting it to 0,0,0

	local clampedVector = Vector3.new(PlayerBall.AssemblyLinearVelocity.X/overSpeed, 0, PlayerBall.AssemblyLinearVelocity.Z/overSpeed)
	
	local Velo = (MovementVector*150 * (dt)) * (1 - clampedVector.Unit:Dot(MovementVector) / MaxSpeed)
	if (Velo.X.."" == "nan") then
		Velo = Vector3.new(0,0,0)
	end
	
	PlayerBall.AssemblyLinearVelocity += Velo
1 Like

You can also test if a number is NaN:

function isNaN(n: number): boolean
    return n ~= n
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.