형들 골드메탈영상보고 배운거 토대 + 구글링통해서 복습겸 마리오 구현하기 하고있는데
유니티 관련 사람들 예기하는거 들어보니까
input 관련 - update() 에 구현
physics 관련(rigidbody, addforce, linearvelocity등) - fixedupdate() 에 구현
한다고 하더라고
그래서 원래 점프관련된거를 전부 update에 구현했다가
다음과 같이 바꿨는데 이게 점프가 안되는데 왜그럴까?
마리오해본사람은 알겠지만 점프 꾹누르면 더 높이 올라가게 코딩했음
public class Mario : MonoBehaviour
{
Rigidbody2D rb;
public float maxSpeed;
float horizontal;
float facingDirection = 1;
bool isGrounded = true;
public float jumpForce = 12f;
public float holdForce = 4f;
public float maxHoldTime = 0.2f;
private bool isJumping;
private float jumpHoldTimer;
private bool smallJump;
private bool bigJump;
private bool stopJump;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update() {
// MOVEMENT INPUT
horizontal = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && isGrounded)
{
smallJump = true;
Debug.Log(smallJump);
}
if (Input.GetButton("Jump") && isJumping)
{
bigJump = true;
Debug.Log(bigJump);
}
if (Input.GetButtonUp("Jump"))
{
stopJump = true;
Debug.Log(stopJump);
}
}
private void FixedUpdate()
{
// Movement
rb.AddForce(Vector2.right * horizontal, ForceMode2D.Impulse);
if (rb.linearVelocity.x > maxSpeed)
{
rb.linearVelocity = new Vector2(maxSpeed, rb.linearVelocity.y);
}
if (rb.linearVelocity.x < maxSpeed * -1)
{
rb.linearVelocity = new Vector2(maxSpeed * (-1), rb.linearVelocity.y);
}
// Jump
Jump();
// Flip
if (horizontal >= .1f && facingDirection < 0 || horizontal <= -.1f && facingDirection > 0)
{
Flip();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Material")
{
isGrounded = true;
}
}
private void Jump()
{
if (smallJump)
{
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
isJumping = true;
isGrounded = false;
jumpHoldTimer = maxHoldTime;
smallJump = false;
}
if (bigJump && isJumping)
{
if (jumpHoldTimer > 0f)
{
rb.AddForce(Vector2.up * holdForce, ForceMode2D.Force);
jumpHoldTimer -= Time.fixedDeltaTime;
}
else
{
isJumping = false;
}
}
if (stopJump)
{
isJumping = false;
stopJump = false;
}
bigJump = false;
}
...
디버그 로그는 제대로 찍힘?
디버깅해보려고 디버그 작성했는데 처음 스페이스를 눌렀을시 3개다 입력이됩니다.
그리고나서는 엔터를 입력해도 getbuttonup만 입력되고 나머지는 입력이 안되네요...
컴퓨터 재부팅 후에 public인 jumpPower의 값을 올리니까 갑자기 작동했습니다. 도움주셔서 감사합니다!
isGrounded는 잘 작동해?
변수가 public 이라 인스펙터창에서 막 0으로 되어있고 그런게 아닐까요
제 컴이 똥이라 그런지 제가 전에 만들었던거 참고하면서 했을때는 분명 inspector창에서 public인 jumpPower 값을 부여했음에도 불구하고 적용이 안되다가 말씀해주셔서 컴퓨터 껐다가 유니티 다시키니까 작동잘되네요... 원래 유니티가 이런가요?ㅋㅋ