์ฝ๋๋ ๊ฐ์ด ์ฌ๋ ค๋ด
์ฐ์ void Update()์ ์๋ ์ ๋ ฅ ๋ฐ์์ฃผ๋ ์ฝ๋
xInput = Input.GetAxisRaw("Horizontal");
yInput = Input.GetKeyDown(KeyCode.Space);
yOutput = Input.GetKeyUp(KeyCode.Space);
์ด๊ฑด ์ ํ ๋ฒํผ ์นด์ดํธ ์ฝ๋ (์๋ update์ ์์)
ย ย ย ย if (grounded)
ย ย ย ย {
ย ย ย ย ย ย hangCounter = hangTime
ย ย ย ย }
ย ย ย ย else
ย ย ย ย {
ย ย ย ย ย ย hangCounter -= Time.fixedDeltaTime;
ย ย ย ย }
ย ย ย ย if (yInput && grounded)
ย ย ย ย {
ย ย ย ย ย ย jumpBufferCounter = jumpBufferTime;
ย ย ย ย }
ย ย ย ย else
ย ย ย ย {
ย ย ย ย ย ย jumpBufferCounter -= Time.fixedDeltaTime;
ย ย ย ย }
์ด๊ฒ ๊ทธ ๋ฐ์ ์๋ ์ ํ ์คํ ์ฝ๋
if (jumpBufferCounter > 0 && hangCounter > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
jumpBufferCounter = 0;
hangCounter = 0f;
}
if (yOutput && rb.velocity.y > 0)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.2f);
}
์ผ๋จ ์๋ํฐ์์์ ์์ง์๊ณผ ๋น๋ํ๊ณ ๋์์ ์์ง์์ด ๋ค๋ฅด๋ค๋ ๊ฒ ๋ฌธ์
๊ทผ๋ฐ ๊ณ์ ํด๋ณด๋๊น ํน์ดํ ์ ์ด ์ข์ฐ ์์ง์ ์๋๋ ์ ํํ ๊ฐ์๋ฐ ์ ํ ๋์ด๋ง ๋ค๋ฆ (์ ํ ๋์ด ๋ง๊ณ ๋ ๋ค๋ฅธ ์ ์ด ํ๋๋ ์์)
์๋ํฐ์์ ์ ํํ ๋ ๋ณด๋ค ๋น๋ exe์์ ์ ํํ ๋ ๋์ด ์ฌ๋ผ๊ฐ
ํ๋ก์ ํธ ์ค๋ ฅ ๊ณ์๊ฐ ๋ฌ๋ผ์ง๋ ์ถ์ด์ gravity y๊ฐ -9.81๋ก ์ก์์ฃผ๋ ๋ณ์ ์ ์ธ๋ void Start()์ ๋ฃ์ด๋ดค๋๋ฐ ๋ฌ๋ผ์ง ๊ฑฐ ์์
Gravity Scale์ 8, jumpForce๋ 33์
์ ๋ฐ ์ด๋ ค์ค ์ ์ ๋๊ฐ ๊ฒใฑ ใ
ใ
ใ
์ ์ง์งในใ
rb velocity ์ ๋ฐ์ดํธ ๋ฌธ์์ ์กฐ์ ํ๋๊ฑฐ ๋๋ฌธ์ธ๊ฑฐ ๊ฐ์๋ฐ
๋๋ ๊ทธ๋ ๊ฒ ์๊ฐํจ fixed update๋ก ๊ฐ์ผํ ๋ฏ
์ ๋ฏธ์ ๊ธ์ ์๋ชป ์จ์ ์ ํ ์คํ ์ฝ๋๋ฅผ ๋ ๋ฒ ๋ฃ์๋ค ๋ณธ๋ฌธ ์์ ํจ
update์์์๋ Time.fixedDeltaTime์ด ์๋๊ณ Time.deltaTime์
ใฑใ ์์ ํจ ๊ทผ๋ฐ ๋ฌ๋ผ์ง๋๊ฑด ์๋ค ํใ ใ ใ ใ
FixedUpdate์์ ์ fixedDeltaTime์ด ์๋๋ผ deltaTime์ ์ฐ๊ณ ์๋๊ฑฐ์?
FixedUpdate์์๋ fixedDeltaTime์ด๊ณ Update์์๋ deltaTime์ธ๊ฑฐ์ผ? ์ดํด๋ฅผ ์ด์ง ์๋ชปํ๊ณ ์์๋ด
ใ ใ ๊ทธ๊ฒ ๋๋ฌธ์ ์๊ธด ๋ฌธ์ ์ธ๋ฏ?
์์ฝ๊ฒ๋ ๊ทธ๊ฑด ์๋ ใ ใ
์ ๊ทผ๋ฐ ์ง๊ธ ํ ์คํธํด๋ณด๋ฉด์ ๋ ๋ฐ๊ฒฌํ๊ฑด๋ฐ exe์์๋ ์ ํ ๋์ด๊ฐ ๋ถ๊ท ์ผํจ; ์ง์ง ๋ญ์ง
๋ด ์๊ฐ์๋ update ๋ fixedupdate ๋ ๋ญ๊ฐ ํ์ด๋ฐ์ด ๋ฌ๋ผ์ ธ์ yOutput ๊ฐ์ด ๋ฆ๊ฒ ์ ์ฉ๋๋ ๊ฑฐ ๊ฐ์. update ํ๋ฒ์ fixedupdate ๊ฐ ๋ช๋ฒ ํธ์ถ๋๋์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง ๊ทธ ๋ช๋ฒ ๋์ ์ ํ ์๋ ๊ฐ์ ์ฝ๋๊ฐ ์ ์ฉ๋์ง ์์์ ์ต๊ณ ์๋๋ก ๊ณ์ ์์น์ ํ๋ฉด ๋ ๋์ด ์ฌ๋ผ๊ฐ ์ ์์ ๊ฒ ๊ฐ์.
์ ํํ๊ณ ๋ค์ ๋ฐ๋ฅ์ ๋ฟ์๋๊น์ง ์คํ์ด์ค๋ฅผ ๋ผ์ง์์๋ ์ ํ ๋์ด๊ฐ ๋ฌ๋ผ?
ใ ใ ์ง๊ธ ์ผ๋จ ์์ธ์ด ์ ํ ๋ฒํผ ๋ถ๋ถ์ด๋ผ๋ ๊ฑด ํ์ ํ์
๋ค๋ฅธ๊ฒ ๋ญ ์์์ง๋ ๋ชจ๋ฅด๊ฒ ๋๋ฐ ์๋ฌด๋ฆฌ ๋ด๋ FixedUpdate์์ ํด์ผ ํ ๊ฒ ๊ฐ์ ๋ก์ง์ ๊ทธ๋ฅ Update์์ ํด์ ๊ทธ๋ฐ๊ฑฐ ๊ฐ์. ๊ทธ๋ฅ Update์์ ์ฒ๋ฆฌํ ๊ฑฐ๋ฉด deltaTime์ ์ด์ฉํด์ ์ ๋๋ก ํ๋ ์์ ๋ง๊ฒ ๊ฐ์ ์กฐ์ ํด ์ค์ผ ํ๋๋ฐ rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.2f); ์ฒ๋ผ ๋น์จ๋ก ๊ฐ์์ํค๋ ๋ก์ง์ ๋ฌด์กฐ๊ฑด FixedUpdate์์ ํธ์ถํด์ผ ๋๋์ด ๋น์ทํ๊ฒ ์ ์ง๋ ๋ฏ. ๊ทผ๋ฐ ๊ทธ๋ฅ ์ ๋ ๊ฒ ๋น์จ๋ก ์ค์ด๋๊ฑฐ ๋๋ ์ ์ด์ ๋ณ๋ก ์ ์ข์๋ณด์
์ ํ๋ velocity ๋ง๊ณ addforce ์จ๋ด์ผ๊ฒ ๋ค
๊ท ์ผํ ๋์์ ์ํ๋ค๋ฉด hang counter์ jump buffer count ๊ฐ์์ํค๋ ์ฝ๋๋ฅผ fixedupdate์์ ํด๋ณด์
ํน์ ์คํ ๋ฒ์ ์ vsync ๊ฑธ๋ ค์์? ๋ง์ฝ 100 ํ๋ ์์ด 50์ผ๋ก ์ ํ๋๋ค๊ณ ์น๋ฉด update ์์ ๋ณ์๋ค ๊ฐ์ด ์์ ๋ณด๋ค ๋ ๋ฐฐ ๋ ๋ฆ๊ฒ ๊ฐฑ์ ๋ ๊ฑฐ๊ณ grounded ๋ ๋ฆ๊ฒ ๊ฐฑ์ ๋๋ฉด ๊ทธ์์ ์นด์ดํฐ ๊ฐ๋ ๋ฆ๊ฒ ๊ฐฑ์ ๋ ๊ฑฐ ๊ฐ์๋ฐ