It does not detect collision. (I have been trying to fix this for 3 days now)
using UnityEngine;
public class Enemy : MonoBehaviour
{
[Header("Ectoplasm Reward")]
public int ectoplasmReward = 10; // Amount of ectoplasm to reward when killed
[Header("Respawn Settings")]
public float respawnTime = 5f; // Time to respawn in seconds
private bool isDead = false;
void OnTriggerEnter2D(Collider2D collisionInfo) // This collision does not detect
{
if (collisionInfo.GetComponent<Collider>().tag == "HurtBox")
{
isDead = true;
// Add ectoplasm reward
StatManager.instance.AddEctoplasm(ectoplasmReward);
// Disable enemy
gameObject.SetActive(false);
// Start respawn timer
Invoke("RespawnEnemy", respawnTime);
}
else // Even this does not run
{
Debug.Log("Collision Detected, Wrong Tag");
}
}
private void RespawnEnemy()
{
// Enable enemy
gameObject.SetActive(true);
isDead = false;
}
}
I think it might have to do with the script that makes my sword swing. It disables the sword at first. then re-enables it for the swings and disables it again..
using UnityEngine;
public class SwordSwing : MonoBehaviour
{
public Transform pivotObject; // Reference to the pivot object
public GameObject sword; // Reference to the sword object
public float swingSpeed = 360f; // degrees per second
public float swingAngle = 180f; // total swing angle
public float cooldownTime = 2f; // cooldown time in seconds
private bool isSwinging = false;
private bool isCooldown = false;
private float currentAngle = 0f;
private float cooldownTimer = 0f;
void Start()
{
sword.SetActive(false);
}
void Update()
{
if (isCooldown)
{
cooldownTimer += Time.deltaTime;
if (cooldownTimer >= cooldownTime)
{
isCooldown = false;
cooldownTimer = 0f;
}
}
if (Input.GetKeyDown(KeyCode.Space) && !isSwinging && !isCooldown)
{
isSwinging = true;
sword.SetActive(true); // Enable the sword
}
if (isSwinging)
{
SwingSword();
}
}
void SwingSword()
{
float angleThisFrame = swingSpeed * Time.deltaTime;
pivotObject.Rotate(0, 0, angleThisFrame);
currentAngle += angleThisFrame;
if (currentAngle >= swingAngle)
{
isSwinging = false;
currentAngle = 0f;
pivotObject.localEulerAngles = new Vector3(0, 0, 0); // Reset rotation if necessary
sword.SetActive(false); // Disable the sword
isCooldown = true; // Start the cooldown
}
}
}
But the sword and enemy both have box colliders and rigidbodys set up (Rigidbody has simulator off because the game is top-down-view)
Here are some screenshots:
Player Screenshots
The player object with the SwordSwing script attached
Sword Screenshots
Enemy Screenshots
The Enemy Script Attached to the enemy
Enemy Rigidbody And BoxCollider
I tried using OnCollisionEnter And OnTriggerEnter but it still wont detect it. What im trying to do is when the player swings their sword at the enemy their Ectoplasm goes up and the enemy gets disabled for respawnTime amount of seconds.
You disabled both Rigidbody2D
via Simulated
When not simulated, any attached Collider2D or Joint2D also do not participate in the physics simulation.
If anything you probably rather wanted to set them to be kinematic
instead