Lasers stay on when I collide with something

Everything else seems to be working fine besides the lasers stay active after the player ship explodes. I am not entirely sure where in what script I would aim to fix that. I have searched Google and tried multiple things but I cant seem to figure it out. I can upload screenshots if needed. Thank you.

Hi @AAndrew,

Welcome to our community! :slight_smile:

Wjat exactly do you mean by lasers? The laser beams that have been shot before the player ship exploded? If so, that’s the desired behaviour because it’s like in the real world.

Screenshots would be great to help me understand the problem you would like to solve.

So what I mean is the lasers coming from the player ship will continue to be shot out after player ship is destroy by collision with another object, if the player dies while the “fire” button is being pressed otherwise they stay off. Seems like it disables control but locks it into the last position it was in at time of collision. How could I disable them from being active at the same time the player ship is destroyed? Its hard to show it in pictures, but I added some and I have added some screens of my scripts.






Thank you for your screenshots. Now I know what you mean, and I agree that this behaviour does not make any sense.

The problem is simple: The emission module gets enabled when the “fire” key was pressed. And if the player gets destroyed, the PlayerController gets disabled. The else-block in ProcessFiring will not get executed anymore.

An equally simple solution would be to have a SetLasersInactive method in the CollisionHandler method. A bunch of components get disabled there, just the lasers are missing.

If you want to challenge yourself, you could create a more elegant solution than copying and pasting the SetLasersActive method. You could outsource the shooting feature to a new class. Then you could make the CollisionHandler call a method in that new object to disable the lasers and to disable the component.

Maybe you’ll come up with an even better idea. :slight_smile:


See also:

Awesome, thank you! That makes sense! I will play around with some options are respond back here with my results!

Okay So I managed to create a separate script for the lasers called LaserController and inside that script is all the information to fire and stop the laser emission, including the input system, and I made a new public method called SetLasersInactive();


I then called the SetLasersInactive(); in my CollisionHandler script and my PlayerController script. I had to set the reference for the new public methods, I did that within the Start() method with the line laserController = (LaserController)GameObject.FindObjectOfType(typeof(LaserController));




In the end of all of it, I did manage to get my lasers to activate and deactivated correctly and they are destroyed after the PlayerShip is destroyed after a collision event. However, the existing lasers that are in the world after death disappear because the entire emission module is being deactivated so I believe I need to make a change to the SetLasersInactive() method to deactivate the firing action instead of just destroying all laser emission that is already out in the world.

Good job so far! :slight_smile:

That’s interesting. I thought the particles would survive when the Simulation Space is set to “World”. However, I never tested this, so it might well be that they get destroyed along with the particle system. In that case, you will indeed have to deactivate the shooting function. Keep your LaserController and modify it.

Hint for a potential solution

An if-statement and a bool should do the job.

I put SetLasersInactive(bool isActive) for the bool and then set it to the true value in my CollisionHandler script in the CrashSequence method. Not entirely sure if that is correct though, it is still working the same. I believe the if-statement would be the thing that would be targeting the firing input system, just unsure on how to write that to coordinate with what I need it to do.

For example:

if (!lasersActive) { return; }

The value for lasersActive could stem from the object which “knows” whether the player is still alive. For example, the PlayerController could contain a public method which returns the state of the player (dead, alive, whatever) or, alternatively, true if the player is still alive, and false if he’s dead.
The PlayerController does not know anything about the LaserController. However, the LaserController references the PlayerController to call the aforementioned method. And you process the returned value in the LaserController object to determine whether the player is allowed to shoot.

This is just a suggestion, though. There are certainly multiple ways to wire the logic up.

I have been giving this a go every day and I just can’t quite figure out exactly what to write. I don’t seem to gain any progress, I may just be way overthinking it but I am unsure where to go with it at this point. Could you please elaborate a little further on your point to help me understand how to go about writing this?

Admittedly, your idea is relatively complex. It’s easy to get lost. If this was my project, I’d draw the logic in a little diagram with pen and paper before diving into coding.

Just to recapitulate the current problem to ensure we are talking about the same: The problem is that the lasers can still be shot even if the player is dead.

Unfortunately, I cannot copy and paste code from screenshots, and reading from screenshots is also difficult for me. I basically forget everything after having deciphered like 3 lines of code.

From what I see and remember, there is a public ProcessFiring method in the LaserController class/object. In the PlayerController class/object, the ProcessFiring method gets called.

Problem: There is no restriction which prevents ProcessFiring from being called. If the PlayerController object gets disabled, the lasers will either be active or inactive depending on the last method call of ProcessFiring. The LaserController does not have any information about anything, so “nothing” will ever happen unless other code explicitely call the methods inside the LaserController object.

There are multiple ways to solve this problem. With your current solution, when the player is dead, the PlayerController object needs to call a method in the LaserController object to have the LaserController disable the lasers. ProcessFiring must not get executed anymore after the PlayerController called the aforementioned method. This means that you need a bool and an if-statement inside the ProcessFiring method so the code block does not get executed if the lasers are meant to be disabled. This bool does not have anything to do with the state of the laser game objects but defines which code is supposed to get executed in which case. Maybe you remember what we did in the Project Boost project with the states of the rocket (alive, dead, transitioning, etc.).


See also:

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

Privacy & Terms