How to use math in code?

So, I basically want to know how to use math to improve code, for example smoothing enemy movement when it is walking? I’m not the best at math so can you link a tutorial or something if possible? Thanks.

Math in code is principaly the use of operators (The basics of using math in code)

For example to add 1 to 2 I can simply do:

local number = 2
local newNumber = nil -- Which is defined to "none".

newNumber = number + 1

Of course, there is plenty of other operators like
- | For Substracting
* | For multiplying
/ | For dividing

For smooth movements, you can use TweenService.

You can learn about operators here: Official Lua Documentation about Operators

Hope this helps!

There is also the ACTUAL math library, which provides complex functions for games.
Here are a few examples:

math.clamp(value, min, max): Returns an integer at or inbetween the specified number value.

math.floor(value): Returns the lowest integer closest to value.

math.rad(value): Converts value into radians, which can be used for CFrame.Angles().

math.deg(value): The standard number type, this converts value in radians back to degrees.

math.pi: Pi, also known as π, the value 3.14159265…, which is used for formulas like circle area.

math.exp(value): Calculates the exponential value of value with e^value, where e is 2.78281…

You can learn more functions with math in the documentation.

1 Like

Oh okay I get it, but could you explain how to get smooth turns like this using math? The person who made this said he used math to calculate the turns and used 1 part at each turn? Because I want to expand my scripting knowledge as much as I can, thanks.

That would include editing the CFrame most likely. From what I’m seeing, that would require math.rad() to make a CFrame.Angles() change the direction that something moves. I haven’t tried making something like this before, so it’s a good guess.

If you want something smooth, you should look towards the TweenService. It has a lot of things that can help smoothen things out.

Yeah thanks for the help i will look into it later

You can also use this for smooth movement instead of TweenService,

 CFrame:Lerp(GoalCFrame, 0.25--[[rigidness]])