Unity 2D Платформер

2075 / Unity / 2D / Гравець / Платформер

 

Platformer2D PlatformerShoot2D
  • Створюємо Пустий об'єкт Player
  • Додаємо BoxCollider2D чи CircleCollider2D
  • Додаємо Rigidbody2D: Gravity Scale = 3, Freeze Rotation z
  • Всередину Player-а додаємо світлину і Пустий об'єкт Ground

 

public float speed = 8;
public float jumpForce = 24;
public Transform groundCheck;
public bool isFacingRight = true;
Rigidbody2D rb;
float horizontal;

void Start()
{
  rb = GetComponent<Rigidbody2D>();
}

void Update()
{
  horizontal = Input.GetAxisRaw("Horizontal");

  if (Input.GetButtonDown("Jump") && IsGrounded())
  {
    rb.velocity = new Vector2(rb.velocity.x, jumpForce);
  }

  if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
  {
    rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
  }

  Flip();
}

void FixedUpdate()
{
  rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}

bool IsGrounded()
{
  Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.transform.position, 0.2f);
  if (colliders.Length > 1) // my collider and ground collider
  {
    return true;
  }
  else
  {
    return false;
  }
}

void Flip()
{
  if (isFacingRight == true && horizontal < 0f || isFacingRight == false && horizontal > 0f)
  {
    isFacingRight = !isFacingRight;
    Vector3 localScale = transform.localScale;
    localScale.x *= -1;
    transform.localScale = localScale;
  }
}