Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannon ball objects do not delete when player misses. #2

Open
kilgoreandy opened this issue Feb 19, 2023 · 1 comment
Open

Cannon ball objects do not delete when player misses. #2

kilgoreandy opened this issue Feb 19, 2023 · 1 comment

Comments

@kilgoreandy
Copy link

When a player or AI misses a shot, the cannon balls falls off the screen. This asset stays spawned on the screen and is never deleted. This will impact performance as the Unity engine will continue to keep up with the assets (this can be many very quickly). The more objects that are spawned that are not deleted, the more impact to the performance.
image

@kilgoreandy
Copy link
Author

one possible fix, set a timeout for the projectile

using UnityEngine;

public class Projectile : MonoBehaviour
{
public int damage;
public bool friendly_projectile;
public float destroy_delay = 5f; // Time to wait before destroying the projectile

private float destroy_timer; // Timer to count down to destruction

private void Start()
{
    // Start the timer when the projectile is instantiated
    destroy_timer = destroy_delay;
}

private void OnCollisionExit(Collision collision)
{
    if (collision.gameObject.TryGetComponent<IDamageable>(out var damageable))
    {
        // Damage the object if it is damageable
        damageable.damage(damage, friendly_projectile);
	    // Reset the timer when the projectile collides with anything
        destroy_timer = destroy_delay;
    }


}

private void Update()
{
    // Update the timer and destroy the projectile when it reaches zero
    destroy_timer -= Time.deltaTime;

    if (destroy_timer <= 0f)
    {
        Destroy(gameObject);
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant