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.

Help with colliders

Discussion in 'Editor & General Support' started by A2GStudios, Dec 12, 2018.

  1. A2GStudios

    A2GStudios

    Joined:
    Nov 21, 2016
    Posts:
    52
    In my game you can use a spear to kill animals, the collider on the tip of the spear hits the collider on the AI and causes damage. Everything works great, but on smaller animals I want to do something different. I want to stab a lizard or small rabbit with a spear and it stick to the end of the spear.

    So how can I make it to where when the spear collider hits the animal collider it will work like a magnet and stick to it, or is there a better way of doing this. I also want to use the same method for fishing.

    Thanks for any and all help.
     
  2. Steve_Stevens

    Steve_Stevens

    Joined:
    May 3, 2016
    Posts:
    34
    Well, if you done your damage properly, it's a base class for anything that receives damage.
    If that's the case, you simply override your damage class in your fish or squirrel class that parents it to the spear.

    If you want help on this, I will be glad to shoot you some code.
     
    Kiwasi likes this.
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Food for thought, how about a component for everything that can take damage (Composition over inheritance)
     
  4. imaginationrabbit

    imaginationrabbit

    Joined:
    Sep 23, 2013
    Posts:
    339
    Couple ways to do this- here's a few-
    1. On hitting the collider gets its transform and set its parent to a transform on the spear tip (If the scale on the parent object is not 1,1,1 the scale will get weird)
    2. On hitting the collider- disable the animal game object and spawn/instantiate a "dead" version of the animal that is a child of the spear tip
    3. On hitting the collider Add/Use a fixed joint component to attach the spear tip and animal(they'll need rigid bodies for this)
    4. Or use a "Constraint"- few assets and I think Unity now has built it ones in new versions
     
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    896

    If you use public void SetParent(Transform parent, bool worldPositionStays), with worldPositionStay as true, then there shouldn't be any weirdness.