hi i want to write a script that make player movement look like csgo
i was finding for a source or tutorial and i found this
https://adrianb.io/2015/02/14/bunnyhop.html
so i converted it to luau and trying to port it into roblox
i do not understand how it work, it giving me “nan vector” instead of normal vector
does anyone know the issue or something?
task.wait(1)
local ground_accelerate = 100 -- Replace with your desired value
local max_velocity_ground = 100 -- Replace with your desired value
local air_accelerate = 50 -- Replace with your desired value
local max_velocity_air = 75 -- Replace with your desired value
local v = Vector3.new()
local Human = script.Parent:WaitForChild("Humanoid")
Human.WalkSpeed = 0
Human.JumpPower = 0
Human.UseJumpPower = 0
local RootPart = script.Parent:WaitForChild("HumanoidRootPart")
local dt = 0
local function Dot(vec1, vec2)
return vec1.X * vec2.X + vec1.Y * vec2.Y + vec1.Z * vec2.Z
end
local function Normalize(vec)
local magnitude = (vec.X^2 + vec.Y^2 + vec.Z^2)^0.5
return Vector3.new(vec.X / magnitude, vec.Y / magnitude, vec.Z / magnitude)
end
local function Accelerate(accelDir, prevVelocity, accelerate, max_velocity)
local projVel = Dot(prevVelocity, accelDir) -- Vector projection of Current velocity onto accelDir.
local accelVel = accelerate * dt -- Accelerated velocity in the direction of movement (assuming FixedUpdate is called every 0.016 seconds)
-- If necessary, truncate the accelerated velocity so the vector projection does not exceed max_velocity
if projVel + accelVel > max_velocity then
accelVel = max_velocity - projVel
end
return prevVelocity + accelDir * accelVel
end
local friction = 5
local function MoveGround(accelDir, prevVelocity)
-- Apply Friction
local speed = prevVelocity.magnitude
if speed ~= 0 then -- To avoid divide by zero errors
local drop = speed * friction * dt
prevVelocity *= math.max(speed - drop, 0) / speed -- Scale the velocity based on friction.
end
-- ground_accelerate and max_velocity_ground are server-defined movement variables
return Accelerate(Normalize(accelDir), prevVelocity, ground_accelerate, max_velocity_ground)
end
local function MoveAir(accelDir, prevVelocity)
-- air_accelerate and max_velocity_air are server-defined movement variables
return Accelerate(Normalize(accelDir), prevVelocity, air_accelerate, max_velocity_air)
end
game:GetService("RunService").Heartbeat:Connect(function(dt2)
dt = dt2
if Human.FloorMaterial ~= Enum.Material.Air then
RootPart.Velocity = MoveGround(Human.MoveDirection, Vector3.new(max_velocity_ground, 0 ,max_velocity_ground))
else
RootPart.Velocity = MoveAir(Human.MoveDirection, Vector3.new(max_velocity_air, 0 ,max_velocity_air))
end
end)