Ship Collisions

When I run the game all ship objects both enemy and player are destroyed on collision. So the health is not affecting them. And the enemies destroy themselves if they collide.


Hi,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

What’s the value of health in the Inspector? And have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe the damage value is too high in the TakeDamage method.

Hope this helps :slight_smile:


See also:

Hi:

The Health of the enemy is 50 and the damage dealer on it is 25. The Health of the player is 50 and its damage dealer is set to 10.

I haven’t run debug logs. I’m not really sure how and where to implement them in the code.

Saw another post in the lesson mentioning to remove this line of code from the health script.

damageDealer.Hit();

I did and that seemed to work. Although with my tiny grasp on C# at the moment, I’m not sure why that line of code was causing the issue.

damageDealer.Hit();

Yeah, well that calls code (Destroy(gameObject) that will destroy any object that is calling it.

Is this code from a lecture? If so, continue on through the lectures and issues are usually ironed out over time.

@JHMGames, damageDealer.Hit() is responsible for destroying the incoming object that hit the one with the Health() component.
This will become a lot more obvious in the next lecture when we implement projectiles, but the general gist is that the component with the health script is taking some amount of damage and the project that caused it will be destroyed.

The way it’s currently set up will cause two ships to annihilate each other on contact, since they both have health components.

They will also deal damage to each other first though. So, if you want them to survive a ship-to-ship collision, then you could either add some additional checks to the if-statement on ln13, or play with the sorting layers to prevent ship collisions entirely (more on that in the next lecture).

I hope that helps.

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

Privacy & Terms