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.
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.
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?
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.
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.
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.
“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.
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?
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.
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 )
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.
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.
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.
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.