Stop exit form Destroying bullets

I’ve been stuck on this longer than I would like to admit.

can someone help, please?

Hi,

If you need help, it would ge breat if you could share more information on what you did and have in your game. At the moment, we do not know anything about your game and something about an ‘exit’.

What exit do you mean? And whose bullets? The player’s? If so, how do your bullets destroy something? If the bullets don’t destroy the walls, there must be a difference between a wall and an exit.

Hi

Sorry, I thought “ask question” this would ask a question about the lecture I’m on.

It’s the tilevania section.

" Complete C# Unity Game Developer 2D Online Course"
The " Level Exit Portal" lecture

The code is exactly the same as Ricks.

I have tried various versions of CompareTag, layermask.getmask.
I will write in shorthand for the examples not actual code.

if(layermask or tag "exit)return
if !enemy return

if enemy
destroy
else
return

my tags and layers were set in unity and entered correctly in code

I’m doing something wrong and I don’t know what.

Yes, that’s right. The tag refers to the video. However, since Rick’s solution works in his video, and since you do not get the expected results, there must be a difference. I’m no clairvoyant, so I have to rely on the information you provide. :slight_smile:

From what I see in your pseudo code, you check for certain things to terminate the method with return;. And if the other game object is an ‘enemy’, you call Destroy. Where is this code? Is it in a script attached to the portal?

If the program ‘skips’ the if-statements for some reason even though you expected the method to terminate early, use Debug.Logs to see what’s going on. For instance, check the tag with a Debug.Log. Maybe the portal does not have the expected tag, or the spelling of the tag in Unity does not match the spelling in your code. There could be many reasons.

Generally, don’t assume that anything works as expected just because the code seems to be correct. In many cases, the problem is where we think it is not, and we often waste a lot of time checking everything but the parts we think are correct.

That’s understandable.

So far I don’t think Rick hasn’t shot the exit, or had an enemy touch it, which in my levels trigger the next level, I don’t know if he addresses that in a later lecture.

I implemented this :

if(!other.CompareTag("Player"))
            {
                return;
            }
            else
            {
                StartCoroutine(LoadNextLevel());        
            }

To stop bullets and enemies from starting the next level in levelExit.cs

This is the current bullet destruction code on the bullet script.

void OnCollisionEnter2D(Collision2D other) 
    {
        if(myRigidbody.IsTouchingLayers(LayerMask.GetMask("Ground")))
            {
                Destroy(gameObject);
            }

        if(myRigidbody.IsTouchingLayers(LayerMask.GetMask("Interactables"))) {return;}
    }

I will do some more lectures and see address the issue.

You are right. This is not a problem Rick has, so he won’t solve it. At least, I don’t remember him shooting at the portal or writing a solution for that.

However, it’s easy to solve the problem. You could, for example, put the exit on a separate layer and configure the layer-based collision matrix, so only the player (who is on the ‘Player’ layer) is able to interact with the exit. The ‘Interactable’ layer could be an option. The player’s bullets are not on the ‘Player’ layer, aren’t they?

An alternative solution would be to check if the other game object has got the ‘Player’ tag. That was your idea. Your solution with the ‘Player’ tag can also be applied to the bullet script. In this case, you would check for the ‘Enemy’ tag, not for the ‘Player’ tag.

If you work with the layers (given that’s possible), you won’t need those if-statements.


A little tip for refactoring your code:

if (!other.CompareTag("Player"))
{
    return;
}
            
StartCoroutine(LoadNextLevel());        

Since return; terminates the method immediately, you won’t need an else because the lines below return; will never get executed if the first if-condition is true. It’s common practice to write the code the way I showed you but your version is valid too.

I didn’t think I needed an else, but being a newb, and desperately trying everything I just went for it haha

Thank you for the help, I will try this out later and hopefully get it working.

Thanks again!

That’s fine. In my personal opinion, it is better to learn to write if and else first before trying to work on one’s coding style. However, since you are already in the TileVania section, I doubt that you are still a complete newb, so I figured I could show you this alternative way without confusing you. :slight_smile:

1 Like

Hi

As you suggested, I ended up stopping them from interacting in the layer collision matrix.
such a simple elegant way of making sure they do not interact. I spent so much time thinking of code
I didn’t remember about the collision matrix.

Thanks again for your help

Much appreciated!

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

Privacy & Terms