Search Unity

Giving damage to enemies with multiple collisions using raycast

Discussion in 'Scripting' started by ChuckieGreen, Mar 22, 2018.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    So I have already sort of asked this question, but I gave my code that I had set up and asked for help in why it wasnt working correctly, and that still never solved the issues. So I am basically getting rid of all my code and hoping someone can help with the best set up.

    So what I have is a enemy prefab, which had different bones as child objects, different bones have different colliders. On the parent there is a health script. What I want to happen is when the raycast hits one of the colliders attached to the bones, the health (which is on the script on the parent) will be reduced.

    The raycast is attached to the gun, but I am basically removing all code for that just now.

    I tried using the unity zombunny tutorial, after my code would not work, and it also did not work, not sure if its because there is not multiple collision boxes on the zombunny.

    The image shows the different child objects and colliders.

    If anyone could please help with this it would be greatly appreciated. I've changed the code so many times and still nothing is working. Only have a week to finish it now :(:(:(
     

    Attached Files:

  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It would help if you said what "not working" means.
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Without knowing more about what's specifically wrong, I'll assume there's an issue with your raycasting. Before doing your raycast, try calling `Debug.DrawRay()` with the same origin and direction you pass to Raycast. This will draw a line in the Scene view (not in the Game view), and it will show you if you're raycasting in the right direction. If the line isn't going where you expect it to go, you're probably raycasting off in some wrong direction.
     
  4. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    well i have completely removed my code now, hoping to start again. so i guess you can just ignore the "not working part". Basically i dont have the code for it, and wonder how it would best be set up
     
  5. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    its definitely not the raycast, I dont really want to post the code I was working with as it will basically just turn into the last post I made and was hoping to start with fresh code (i was hoping someone would be able to help me set up new code) if you think it would be easier to see the code I was working with (or one of the pieces of code) i can post the newest version I have.
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Why are three characters parented to "VampireBlood"?
     
  7. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    I have no idea, it is from the asset store, I just needed something for this uni project so grabbed this. Could this be the issue?

    EDIT: I believe all the collision boxes are set to bones that all all attached to the child called Character002Hub002
     
  8. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Are there three characters in the scene? Do they share a skeleton or something, or are there three sets of bones?
     
  9. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Well, maybe just include the single line you're using to perform the raycast? Without seeing any code, I can just suggest:
    • Are you using Raycast, or RaycastAll? Is it possible you're hitting something else before you hit one of the colliders?
    • Are you using a layer mask on your raycast? Do you know how to create the layer mask properly? (It's a bit confusing at first.)
    • Is Raycast returning true or false? If it's returning true, what object is it colliding with?
     
  10. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    They are different bones

    Video will make it easier to see what everything is
     
    Last edited: Mar 22, 2018
  11. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    well as I said ive changed the code many times, but this is the last version i tried. Ive just included the raycast and what should happen with it. The raycast is working as i get all the debug messages up to "enem" So I guess the issue would be with the get component part, but I dont understand why, its basically how the unity tutorial says to set it up. also if you check the else if statment, this time i try getting the parent to see if that works, it doesn't (that was just to check if i needed to get the parent or not)

    Code (CSharp):
    1.  if (Physics.Raycast(ray, out hit))
    2.                 {
    3.                     Debug.DrawLine(transform.position, hit.point);
    4.                     transform.LookAt(hit.point);
    5.                     Debug.Log("RayCastOut");
    6.  
    7.                 if (hit.collider.gameObject.tag == "VampBody")
    8.                 {
    9.                     VampireHealth enemyhealth = hit.collider.gameObject.GetComponent<VampireHealth>();
    10.                  
    11.                         Debug.Log("HitWithBody");
    12.                     if (enemyhealth != null)
    13.                     {
    14.                         Debug.Log("enem");
    15.                         enemyhealth.TakeDamage(20);
    16.                        
    17.                     }
    18.                 }
    19.                 else if (hit.collider.gameObject.tag == "VampHead")
    20. {
    21.                     Transform parent = hit.collider.gameObject.transform.parent;
    22.                     VampireHealth enemyhealth = parent.GetComponent<VampireHealth>();
    23.                     enemyhealth.TakeDamage(50);
    24.                 }
     
  12. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    It's very likely that the VampireHealth component is on a parent object of the collider you hit. Is the VampireHealth component on the "VampireBlood" game object? If so, calling GetComponent on the game object of the collider you hit won't find it. You'll need to use a different approach to find that component. A somewhat fragile approach would be to try this first of all:

    Code (CSharp):
    1.  hit.collider.gameObject.transform.root.GetComponentInChildren<VampireHealth>();
    I say that's fragile because if you put your VampireBlood game object into some other parent game object, then 'root' will be that parent game object. But in your case, it's likely just the VampireBlood object. But that should confirm whether you can find the component.
     
  13. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    OK that is working, so thanks so much for that. Ive tested with 2 of the vampire prefabs in the scene and I believe they both seem to be keeping their own health.
    And your code helped me solve my animation issue as well. Thank you for the help, it is really appreciated
     
  14. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    GetComponentInParent<VampireHealth>() would go up the tree from the collider until it found the health. A little more future-proof incase you move all of your objects to some environmental container GameObject, like say your level data.
     
    dgoyette likes this.
  15. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    I did try just using GetComponentInParent<VampireHealth>() but it didnt seem to get the health script.