Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Enemy not taking damage

Discussion in 'Scripting' started by matthewcooper, May 16, 2020.

  1. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    Could someone help me with this? Whenever I attack the enemy, they don't take damage, my weapon is a part of my character not a separate object, I don't know if that affects anything, I know it's only made to print the health at the moment but it's not even doing that, I am also building for mobile if that makes any difference.
    Combat code: https://pastebin.com/UVKn0KsN
    Health code: https://pastebin.com/Lxbzvq10
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Why use a SphereCastAll if you only intend to find the closest enemy? Why check if the hit distance is below 10000 units, if the SphereCast itself only returns hit objects 2.0 units away (as you specified)? Why not use a layer, if you only want to hit specific objects?

    I'm pretty sure something is getting skipped, or overwritten. Define a layer for enemies, do a normal SphereCast on this layer only. If the hitInfo contains something, that's the nearest enemy that was hit. Get its health component and damage it. That's all there is to it. Your code seems unnecessarily complicated, and thus is prone to bugs.

    Edit: Also, please post code examples directly here, using code tags.
     
  3. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    I'm happy to do it another way if there is another way, I know how to code it for PC but I'm new to developing for mobile, also there would be multiple enemies, potentially surrounding you.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Other than inputs there is not a whole lot of differences between a PC implementation and a mobile implementation. Why does your approach for a PC implementation not work for mobile?
     
  5. matthewcooper

    matthewcooper

    Joined:
    Apr 25, 2019
    Posts:
    40
    When I do combat for PC I base it on having a mouse, I click on the enemy to attack with the mouse which obviously I can't do on mobile. If it really helps then I could get the code.