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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Need Help] Destroying parts of a clipping object

Discussion in 'Scripting' started by Zero101, Aug 30, 2016.

  1. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    I have an enemy that has a transparent red cone under it for detecting the player,but the cone is going throu walls, that I do not want to happen,is there a way to destroy part of an object or atleast hide part of an object that is hitting going throu a wall?
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    If you're only trying to keep the OnTrigger functions from firing from collisions through walls, then don't bother in my opinion. It's far easier to keep the cone just how it is, then use a raycast (guard's head to the character's chest), several raycasts (guard's head to character's head, chest, hands, and feet), or a spherecast (guard's head to character's chest, but much wider area) to determine visibility, depending on the accuracy required. The results of the cast would actually be what determines "Seen" or "Not Seen", and not just the cone area.
     
  3. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    3 Thigs:
    1. The cone i mentioned i want it to be visible to the player because using a light isn't as visible as a transparent object
    2. I already had planned to use raycast
    3.The cone i mentioned, i want the cone not to go throu walls as it will confuse the player at start and it will give the position of an enemy.

    To understand better imagine a roof , a player under the roof, and a point X over the roof.From point X towards the player there is a VISIBLE cone. I want the cone not to extend into the player's room(under the roof) and the cone to be projected only where it can, like a light, but a cone.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    A couple of things I can think of. The operation you're talking about is a boolean subtraction method, and it's not really trivial. There was a really promising project a few years back in this area for Unity, and I was following it rather hopefully, but it never actually made it to the light of day. More recently though, this asset popped up on the store- I haven't had a chance to check it out as I've had absolutely no need for boolean operations lately. Frankly, I'm not even sure it can do realtime adjustments on a frame-by-frame basis, and even if it can it feels like the kind of thing that can be easily too expensive to be practical, especially if you're having to do it to several cones in a scene simultaneously.

    Alternatively, you can run the calculations yourself. Cache a copy of the original mesh filter component, make another trigger component for the cone that reacts to the walls' layer, then use OnTriggerStay combined with simple adjustments to the cone mesh's vertex locations and such each frame. It wouldn't wouldn't be that expensive I don't think for basic adjustments, but the math could get complicated fast if you want to accommodate hitting the corner of a wall and creating new triangles and such.

    Another method could possibly be utilizing the way that cameras render layers of objects- perhaps having the room the player is currently in (the whole room, yes) render on a different layer than every other room, adjusting automatically as the player moves around, and have that layer render on top of all of the others, effectively cutting off any clipping through the walls at all. This could create a lot of problems, but it really depends on the design of the game.

    You could also try giving each room a big trigger collider to set an ID number and then disable the renderers for all cones with different room IDs (only the cones in the current room could be seen), or make sure that you set the auto-pathing so that they just don't clip through the walls, or you can dynamically adjust the height of a cone so that it shrinks itself whenever it comes into contact with a wall collider.

    Just a few ideas- I hope you figure it out!

    EDIT: Immediately after posting, I found this. Looks like it might be exactly what you need, but shaders aren't really one of my strong suits.
     
    Last edited: Sep 1, 2016