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

Detect if player is inside or outside of looped path

Discussion in 'Cinemachine' started by Enec0, Mar 19, 2019.

  1. Enec0

    Enec0

    Joined:
    Nov 6, 2017
    Posts:
    8
    Hey,

    I created a looped path using the "Cinemachine Smooth Path" script. The dolly on the path is carrying an audio source. Whenever the player is WITHIN the circled path, I want the audio source to be attached to the player. As soon as he exits the circle, the audio source should be attached to the dolly again.

    Is there a way to determine if the player is on the inside or outside of the looped path?

     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    Can you make a circular trigger collider that approximates it?
     
  3. Enec0

    Enec0

    Joined:
    Nov 6, 2017
    Posts:
    8
    The idea behind my approach is to be able to adapt this to any shape. I want to set up audio ambient zones corresponding to the shape of the level and the landscapes. The circular was just an easy example. So working with regular trigger collider shapes would not fit my approach. Here is better example of what I'm trying to do:



    Whenever the player is outside the shape, the 3D audio source is travelling along the dolly path at the closest position to the player. The player hears a distinct located sound from that area. As soon as he enters the shape and crosses the dolly path, the audio source gets attached to the player and travels along with him. Now he is fully immersed by the sound because it can't be located anymore and he is right at the center of it. As soon as the player crosses the dolly path again and leaves the area, the sound gets attached back to the dolly.

    This would be very useful for something like forest sounds, lakes, villages etc. From the distant you hear a 3D sound and once you are inside you will be immersed as the sound travels with you.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    Well, out of the box there's no built-in feature for what you're asking. You could make a custom script to do something like this:
    1. Project a ray from the player's position through an arbitrary point inside the loop (e.g. the center).
    2. Count how many times the ray crosses the path. If it's even, the player is outside the loop, if it's odd, the player is inside. For this calculation, you could approximate the path by a series of straight segments connecting the waypoints.
    Looking at your image, one thing comes to mind: the "closest point on path" algorithm doesn't like concave shapes. If the path forms an arc around the player, the "closest point" becomes unstable. To understand this, imagine this extreme case: suppose the path is a perfect circle, and the player is in the center. Where is the closest point? If the player moves back and forth even a tiny amount, the closest point will zoom around to opposite sides of the circle. Not a problem for when the player is inside the loop, but there's that pocket in the center-right of your image where you'll get that situation outside of the loop. You might want to think about how you're going to handle that.
     
  5. Enec0

    Enec0

    Joined:
    Nov 6, 2017
    Posts:
    8
    I see, this is a little bit more complicated. Using a raycast may not work all the time as it can be blocked by other game objects on the map. Or is there an option to cast through everything and only take the path into account?

    I though of another option: What if all the waypoints of the path were connected to each other with a mesh render like this:



    Then the mesh could work as a trigger collider and from there on I can set the position changes of the audio source.

    And yes, I also figured that the closest point to the player on the path is a little bit sensitive to certain shapes. Therefore I will simply adjust the shape a bit more roughly to avoid bouncing audio.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    Last edited: Mar 20, 2019