2D 캐릭터 점프시 오브젝트를 뚫고 올라가 그 오브젝트에 착지하는 방법.
유니티 레이어 마스크(Layer Mask)의 개념과 레이어간의 충돌 무시 사용법.
Physics2D.IgnoreLayerCollision(layer1, layer2, true/false)
#IgnoreLayerCollision #LayerMask #레이어간 충돌무시
[PlayerControl.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
Rigidbody2D rb;
float moveX;
[SerializeField] [Range(100f, 800f)] float moveSpeed = 400f;
[SerializeField] [Range(100f, 800f)] float jumpForce = 500f;
int playerLayer, groundLayer;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
playerLayer = LayerMask.NameToLayer("Player");
groundLayer = LayerMask.NameToLayer("Ground");
}
// Update is called once per frame
void Update()
{
moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
rb.velocity = new Vector2(moveX, rb.velocity.y);
if (Input.GetButtonDown("Jump"))
{
if (rb.velocity.y == 0)
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Force);
}
if (rb.velocity.y > 0)
Physics2D.IgnoreLayerCollision(playerLayer, groundLayer, true);
else
Physics2D.IgnoreLayerCollision(playerLayer, groundLayer, false);
}
}
댓글