Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Cinemachine collder distance limit problem

Discussion in 'Cinemachine' started by Ninamut, Jul 23, 2020.

  1. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    Hi!
    I'm using a freelook camera with a Cinemachine Collider for my third-person game.
    Since there are a lot of trees with the capsule colliders in my scene, I don't want the camera to be pulled forward every single time when a tree is in line of sight (Transparent Layers isn't an option, because I don't want my camera to go through trees freely). So I adjusted the distance limit (set it to 2). It works fine with trees in LOS. But the camera now falls through the objects that are not thick enough and gets behind them (sometimes even if these objects aren't in the LOS in the first place). It happens whenever my character is too close to them. I think that Cinemachine does exactly what it should do and there's no bug (although at first, I thought that distance limit works ONLY when objects get between the camera and the character), but this definitely isn't a behavior I need. In my case - the camera falls through the mountain walls (terrain). When distance limit is set to 0 that doesn't happen at all.

    Is there a way to avoid this?

    The ideal solution would be the ability to choose layers that are impenetrable no matter what, or layers for which distance limit will always be "the actual distance to the target". But... there's nothing like that in the options.
     
    Last edited: Jul 23, 2020
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    I think you can use transparent layers for the trees. The camera will not pass through a transparent layer, but it will act as though it can see through it.

    You can also consider setting the minimum occlusion time, which allows the collider to ignore passing occlusions (like trees and poles) unless you park the camera smack behind one and leave it there.
     
  3. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    Oh wow, Transparent layers works! BUT Only if the tree is a game object, separate from the terrain. But I placed trees using terrain tools, so... the camera is able to pass through them and doesn't behave like it should, when "Preserve Tree Prototype Layers" is on. Is there a way to solve this problem?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    I don't quite understand your description of the problem. Perhaps you can post some visuals, or explain a little more?

    Regarding the character getting too close to things: have you set the Minimum Distance From Target option to be large enough to clear the character?
     
  5. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    There is a tree prefab I placed as a game object. I set this tree's layer as a "Transparent layer" in the Cinemachine collider settings. And as you said, in this case, the camera isn't able to stay inside the tree mesh (or the collider), it jumps out of it forward whenever this tree is too close to the camera.
    But when I place the tree on the terrain using the "Paint trees" tool (I turned on the "Preserve Tree Prototype Layers" option in the terrain settings) the camera won't do that jump forward when these trees are in the line of sight, it just stays inside the mesh, as if I'm using the "Ignore Tag" option.

    If I set Minimum Distance From Target large enough camera just ignores everything that is close to the character and goes through the objects. So I keep it at a minimum.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    Interesting. Thanks for the description. I'm not familiar with exactly what the terrain painting does, and will have to do some research before I can answer you.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    one question: the result of the tree painting is, I assume, some kind of mesh. What layer is that mesh on?
     
  8. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    I used the same tree prefab in both cases I described (I placed it as a game object and used it in the "Paint Trees" tool). I made a layer named "Trees" and assigned it to this prefab.
     
  9. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    "Preserve Tree Prototype Layers" option in terrain settings is turned on in this case. It preserves tree prefab's original layer ("Trees" in my case). If I turn this option off, all trees that were placed using the "Paint Trees" tool will be the same layer as the terrain itself.
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    So, the paint trees tool is instancing a bunch of meshes, with colliders around them, and putting them on the Trees layer? Are you sure each tree object has a collider?
     
  11. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    First question: Well, I guess so. Second question: Yes. My character would walk through them otherwise, wouldn't he? And he does exactly that when I turn the capsule collider off in the tree prefab.
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    True. Can you show me the inspector of one of them?
     
  13. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
     

    Attached Files:

  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    Well, I can see no reason why the CM collider would not work perfectly well with this. As far as it's concerned, it's just another capsule collider, with no connection to any terrain. I'll have to try to repro this. I'll look into it in the coming days (it's late for me now, and I'm cooking dinner :) )
     
  15. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    I hope that you'll be able to track this issue and solve it. Enjoy your meal! :)
     
  16. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    Btw it's pretty easy to replicate. I just did it in the new project (2019.4.2f1) using the third-person controller from standard assets, one free SpeedTree, and Cinemachine 2.6.0.
    Project is a bit big, so here's the link
    Two trees on the terrain. One is placed as a game object, the other one is placed using terrain's "Paint trees" tool.
     
    Gregoryl likes this.
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    Thanks for the upload. As far as I can tell, it works correctly. The FreeLook can see through the trees, but when you position it so that the FreeLook would be inside one of the tree's capsules, the FreeLook is pushed out.

    Since you have the collider's damping set to nonzero, it's possible that for a few frames the camera will be inside the tree as it damps its way out. Turn off the damping to stop this from happening.

    It's also possible that the tree trunk's collider capsule is slightly small. Try enlarging its radius a little.
     
  18. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    No, sorry, it doesn't At least in my case. That's weird. I can clearly see that the camera stays inside the tree that was placed with the terrain "paint trees" tool. It's not like it lags then jumps out of the collider. It 100% stays inside. There's a clear visual difference. Here's the video I recorded right now: link

    The first time I test the tree that was manually placed as a game object. Everything works as it should.
    The second one is the tree placed on the terrain with the "paint trees" tool. The camera ignores the tree completely.

    It is clear that the camera just ignores the second tree and gets stuck inside the mesh. It's even more obvious in my project where threes use a double-sided shader, I can see that the camera doesn't jump out and stays inside. And yeah, I checked the collider.
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    The CM vcam icon shows where the camera wants to be, not where the camera actually is if the CM Collider moves it.

    If you put the Scene View in Isometric top-down, you can see what's actually happening when the FreeLook wants to be inside the tree:

    upload_2020-7-27_11-43-7.png

    As you can see, the CM icon is inside the tree's capsule, but the camera has been pushed out.
     
  20. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    I know what I saw. Camera frustum also stays inside the tree. You can SEE in the video that camera CLIPS through the mesh. Damping is set to 0. It doesn't do the jump! It's more than obvious with a double-sided shader in my other project.
     
  21. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    In the project that you sent me, were both trees created with the tree painter? Is it possible that I'm checking the wrong tree?
     
  22. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    You should check both trees ) Broadleaf_Mobile gameobject is the one you probably trying to check. One is placed as a game object (the left one, Broadleaf_Mobile in the hierarchy) and the other one, the right one, is placed with the tree painter.
     
  23. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    ok, so where is the collider capsule of the second tree?
     
  24. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    The collider gizmos do not show up if the "paint trees" tool was used. I think it's a part of terrain somehow (the tree painted with the tree painter tool, I mean). But the collider is there, you can check it, just bump into the tree. If you turn the collider off in the prefab of Broadleaf_Mobile (CollisionObject0 and CollisionObject1) and save all the changes to prefab, the character will be able to go through both trees, since they use the same prefab.
     
    Last edited: Jul 27, 2020
  25. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    Yes, with the second tree I am able to repro your issue. It happens only if you include Trees in the Transparent Layers.

    I've tracked it down to an inconsistency in the way the Physics system returns colliders, depending on whether they're painted into terrains or not. I'm following it up with the Physics team.

    Thank you for your patience in helping me see this issue.
     
    Ninamut likes this.
  26. Ninamut

    Ninamut

    Joined:
    Jun 22, 2016
    Posts:
    17
    Ah, good to know! Thanks for that notification. Meanwhile, I had to replace all of the trees that could get in the way of the camera (with the ones that are placed as game objects that are separate from the terrain). Luckily, I don't have many trees that get in the way of the camera in my project so that I can place them without using terrain tools... But it may not be the best solution for some other developer, so it's good to know that this issue won't remain without attention. Thanks!
     
    Last edited: Jul 27, 2020
    Gregoryl likes this.
  27. SaroVati

    SaroVati

    Joined:
    Jun 7, 2015
    Posts:
    5
    This is the exact issue I am still encountering. Is there an update / fix on the terrain tree behaviour?
     
unityunity