Игрок проваливается сквозь землю в юнити 2d
Что делать?
скрипт земли:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ground : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>());
}
}
}
скрипт игрока:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 5f;
public float runSpeed = 10f;
public float jumpForce = 10f;
private bool isGrounded;
private Rigidbody2D rb;
private BoxCollider2D boxCollider;
void Start()
{
rb = GetComponent<Rigidbody2D>();
boxCollider = GetComponent<BoxCollider2D>();
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
Vector2 movement = new Vector2(horizontalInput, rb.velocity.y);
if (Input.GetKey(KeyCode.LeftShift))
{
movement.x = runSpeed;
}
else
{
movement.x= speed;
}
if (Input.GetKeyDown( KeyCode.Space ) && isGrounded)
{
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
rb.velocity = movement;
}
void FixedUpdate()
{
isGrounded = Physics2D.BoxCast( boxCollider.bounds.center , boxCollider.bounds.size, 0f, Vector2.down, 0.1f, LayerMask.GetMask("Ground"));
}
}
Включить колайдер и риджидбади. Либо же выключить риджидбади у игрока
Отключить скрипт земли.