Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Dynamic Collider Box Between Enemies

Discussion in 'General Discussion' started by denali95, Feb 17, 2023.

  1. denali95

    denali95

    Joined:
    Nov 6, 2016
    Posts:
    78
    I'm working on a boss fight where the boss can launch minion robots that go after you. After walking around the player, these robots can then transform into mines that explode when the player gets too close. I also want the minion robots to have an ability where they can limit the player's movement by shooting lightning between each other, causing damage when the player hits it. My original plan for this was to have a dynamic box collider which would stretch in between robots, but this seems a bit tricky given I can't just give it points like a line. I'm thinking I could also potentially rework this to essentially function with raycasts, where I would raycast from one bot to another and see if the player interrupts that ray, thereby receiving damage. What would be the most logical way of handling this?
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,084
    The lightning should be a raycast as that will be the simplest thing to implement and the easiest as well. However, if you wanted the lightning to have a sort of "thickness" to it, you could also use pretty simple math to place a sphere collider on the closest part of the raycast between the two robots.

    edit: or you could use a spherecast, which makes dramatically more sense than what I just suggested.
     
    Last edited: Feb 17, 2023
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,617
    You really don't need to give it points like a line. It's simple math:
    • The center of the box should be at the mid point between the two 'bots. To get that, add their positions together and divide the result in half.
    • The length of the box is the distance between the two 'bots. To get that, subtract one of their positions from the other and get the magnitude of the result. The width and depth of the box are just whatever you design for the lightning effect.
    • Orientation is mathematically a little more tricky, but Unity can do it for you: simply call transform.LookAt(...) towards one of the 'bots.
    That being said, I agree that using casts make a lot more sense here. As a tip related to that, you can use Vector3.Lerp(...) to get a position on a line between the two bots.

    Depending on how the rest of my game is coded, I might also just do the whole thing with some geometry math.