Enemies not getting hit

Hey everyone,

For some reason, my enemy ships are almost not getting hit (very rarely they do): there’s no effect, and I can’t destroy the object. But I can crash in it. Meanwhile, the sphere placeholder behaves as expected.
The issue exists for all the ships I added,
Manually adding rigidbody and simple box collider didn’t help.
The Enemy script looks same as Rick’s.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class NME : MonoBehaviour

{

    [SerializeField] GameObject deathVFX;

    [SerializeField] GameObject hitVFX;

    [SerializeField] Transform parent;

    [SerializeField] int scorePerHit = 15;

    [SerializeField] int hitPoints = 5;

    ScoreBoard scoreBoard;

    void Start()

    {

        scoreBoard = FindObjectOfType<ScoreBoard>();

        AddRigidBody();

    }

    void AddRigidBody()

    {

        Rigidbody rb = gameObject.AddComponent<Rigidbody>();

        rb.useGravity = false;

    }

    void OnParticleCollision(GameObject other)

    {

        ProcessHit();

        if (hitPoints < 1)

        {

        KillEnemy();

        }

    }

        void ProcessHit()

    {

        GameObject vfx = Instantiate(hitVFX, transform.position, Quaternion.identity);

        vfx.transform.parent = parent;

        hitPoints--;

        scoreBoard.IncreaseScore(scorePerHit);

    }

    void KillEnemy()

    {

   

        GameObject vfx = Instantiate(deathVFX, transform.position, Quaternion.identity);

        vfx.transform.parent = parent;

        Destroy(gameObject);

    }

}

Can you help me find where the issue is?

UPD: it became better after I’ve set max collision shapes to a greater number than max particles. But how do I balance these settings to keep the game working?
The formula “Max collision shapes = max particles * laser numbers” looks even better

Hi Peter,

How fast are your lasers? Test the shooting with slower lasers to see if the behaviour improves.

A Rigidbody for moving objects with colliders and a BoxCollider was a good idea. I would recommend to keep these things. In the Rigidbody component, you could try to set the Collision Detection to Continuous. And if that did not help, test the other options. The default value is “Discrete”.

Hi Nina,
I tried to change the laser speed and even that didn’t work. It seems that there are just too many laser particles in each moment of time, and too few collisions for them. The laser prefab had max particles set to 1000 with default 256 collision shapes in the Collision component. I decreased the first parameter to 200 and set shapes to a maximum of 2048, and it works about fine.
But there’s a weird thing: even after the collisions exceed the number of particles, their increase affects the scene. In my example, 2048 with max 200 particles are still better than 1024 with the same amount.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms