- Home /
Turning pieces of code into a method
Hello everyone. I was wondering about using methods to group some lines of code. For example:
void FixedUpdate()
{
if (canJump)
{
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
canJump = false;
onGround = false;
}
}
turning into this
void FixedUpdate()
{
Jump();
}
private void Jump()
{
if (canJump)
{
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
canJump = false;
onGround = false;
}
}
Do these both work exactly the same? Are there any benefits/downsides of doing so, besides making code more readable/cleaner?
Comment
Best Answer
Answer by logicandchaos · Apr 10, 2020 at 12:30 AM
yup that is how you do it and it doesn't just make it more readable abs cleaner it makes it easier to debug and maintain, it makes it more modular and portable, increases code reuse. Code reuse is a really great quality.
Your answer
![](https://koobas.hobune.stream/wayback/20220612230947im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Moving and Attacking 0 Answers
Learning Unity Methods 1 Answer