I tried getting my enemy to fall underneath the ground and when it reaches Y: -20 it destroys which is in a separate code. The enemy. is not moving when my Player jumps on top of it. Here is the code where I disabled the BoxCollider: using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Player_Move : MonoBehaviour {
public int playerSpeed = 10;
public int playerJumpPower = 1250;
private float moveX;
public bool isGrounded;
public float distanceToBottomOfPlayer;
private void Start()
{
Data_Management.data_Management.LoadData();
}
// Update is called once per frame
void Update()
{
PlayerRaycast();
PlayerMove();
}
private void PlayerMove()
{
//CONTROLS
moveX = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
Jump();
}
//ANIMATIONS
if (moveX != 0)
{
GetComponent<Animator>().SetBool("IsRunning", true);
}
else
{
GetComponent<Animator>().SetBool("IsRunning", false);
}
//PLAYER DIRECTION
if (moveX < 0.0f)
{
GetComponent<SpriteRenderer>().flipX = true;
}
else if (moveX > 0.0f)
{
GetComponent<SpriteRenderer>().flipX = false;
}
//PHYSICS
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
}
void Jump()
{
//JUMPING CODE
GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);
isGrounded = false;
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
public void PlayerRaycast()
{
{
RaycastHit2D rayUp = Physics2D.Raycast (transform.position, Vector2.up);
if (rayUp != null && rayUp.collider != null && rayUp.distance < 0.9f && rayUp.collider.tag == "Lucky_Block")
Destroy(rayUp.collider.gameObject);
}
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down);
if (hit != null && hit.collider != null && hit.distance < 0.9f && hit.collider.tag == "Enemy")
{
GetComponent<Rigidbody2D>().AddForce(Vector2.up * 1000);
hit.collider.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.right * 200);
hit.collider.gameObject.GetComponent<Rigidbody2D>().gravityScale = 8;
hit.collider.gameObject.GetComponent<Rigidbody2D>().freezeRotation = false;
hit.collider.gameObject.GetComponent<BoxCollider2D>().enabled = false;
hit.collider.gameObject.GetComponent<Enemy_Move>().enabled = false;
}
if (hit != null && hit.collider !=null && hit.distance < 0.9f && hit.collider.tag != "Enemy")
{
isGrounded = true;
}
}
}
It starts on public void PlayerRaycast()