Unreal Engine 5 - Smooth Camera Collision

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll learn how to create a smooth camera Collision system the camera will smoothly interpolate between colliding points instead of snapping back and forth so here we are in the engine uh I have a block out of a level just a a test level set up and I have the character here uh in this video I have everything already set up so I'll be going step by step through the functions I've implemented and see how it works uh so I'm going to be using the controller to move the character around and the camera so it's a bit smoother to look uh look at and you'll see uh and the lines you see here right now are the debul lines so you don't have to worry about this at the moment we can get rid of them later so as you can see we have a camera attached to a spring arm and the spring arm is positioned uh in this case on top of the character's head you can see where the uh sphere traces come originating from can see here like so spring arms here and while we're moving around normally in a typical 30 third person template character it would be jumping back into the like into this position snapping out instantly and then snapping in instantly cuz as you can see here it's kind of smoothly interpolating so just for a comparative reference we'll see we'll try and see how it looks like in the default version like this so if I'm going through an area where there's like holes in it normally this would happen so it can be a bit jarring at times also like so yeah so you might actually sometimes get lost where your characters are where your character is uh let's go back so first for this system to work we are going to have to turn go into the spring component and in under camera Collision do Collision test test we want to check it false and then all we need to do is create one function to run on tick and here I just have it set up as a custom event you can later on uh move this all into a separate function but here just for the sake of uh easier demonstration of the debugging and on stuff like that I have it set up as a custom events that's running on Tech so we create a camera Collision event and here we want to do a sphere Trace by Channel and from the starting point you want to get the spring arms world world location and for the end point we're going to do a little bit more work than simply getting the camera's location uh we'll get back to that in a minute and for the radius we just want to create a variable called probe radius and set a value to something sensible like 20 or you can change this to your preferences and of course the trace channel will be set to camera just to keep it more uh in line with the other Collision matrixes Matrix uh with all the with the other Collision Matrix items and also we want to ignore uh this character this character right we don't want it to collide with this character so here I just close this for in a bit here we just want to make an actors to ignore array and then add a self reference which in this case would be the blue uh character blueprint you're working with and here for the sphere Trace we want to start obviously where the spring arm is in this case that will be here the pivot point of the the or of the spring arm the the origin of the spring arm and it's relative pos position to the Capal component and that'll be the starting point and to the end point we want to get a Direction so basically we're trying to get the red line here so the red line we're going to uh subtract the world location of the camera uh with the spring arms World location and then we're going to get a vector of it we're going to normalize it so what this does is we just get a directional ve Vector so we get a direction of uh that leads from the follow camera to the spring arm I think if is it the other way but yeah I think it's the from the follow camera spring arm and then after we get that direction we multiply it since we have it normalized we just add to we just have to need to we just have to create a uh set distance distance to it so in this case we're going to use as default spring arm length and this is a variable I set on begin play don't worry about this this is just a something a demonstration purpose function I'm just going to move this around here and the on begin play next to the controller stuff and input stuff we get the spring arm Target length and then set it to the this value uh this variable default spring arm length so whatever you set here it's going to be setting this variable and then this is going to be a distance if you want this to be more different to your spring arm you can just change it to a variable or switch it up into any variable to your uh need but for this uh video I have it set up as the spring arms default uh starting uh length and then uh I'll just turn off the debug here and that's all you need to do for the sphere tracks and then we get the brake hit result you can get this by searching for break like so and then if it's blocking something we want to get the location of the impact point so for example this is what's going on this is where the sphere Trace let's just turn it back on sorry so here you can see this red dot that just showed up this red dot right here you can see this is the impact point we want to get the location of that to use it later on for other uh uh variables we need to set or get so obviously if it's hitting something we wanted the camera to move in front of the the this uh pillar let's see if the character is here we want the the camera to set its position here so we need to get the impact Point here we get that and then we're going to adjust it uh by getting the distance we're going to get the distance of the spring arm to the impact point so here it will be from this point to the impact Point that's the distance we're going to get and then we're going to uh subtract it by the probe radius just to kind of pull it out pull it up front a little bit this you might not need maybe but I just have it so it's EAS so it doesn't it pre uh prevents clipping to the wall uh in certain cases and then this is the main part of this system we're using the F interp to node so every tick this is running every tick right so here just make this a bigger here we're getting the current spring arm in this tick and then we have the target uh uh length this is going to be the target length which like I mentioned is this distance so we want the spring arms Target length uh as as it is right now we want to get it and then we're going to set the Target to where it's colliding to the wall if it's set to a wall to the wall and then Delta time we're just going to use get world of Delta seconds this also you can just search for get World Delta seconds and you can get it here and then we we're going to have an interpolation speed this is a custom function I created I'll get back to it in a minute and then we're going to return it and then that return value will be setting the spring arm in this tick and then go we go back here check again if it's colliding and so on and so forth and however if there's nothing blocking we want it to go back to its default length for so example our basic our base Baseline uh camera distance to the character is I mean the spring arms length is 500 let's say then we want it to go back to 500 right but we only want it this exist here because we only want it to go if it's uh not the same so we want this set happening not constantly so if we basically this works too but the thing is I just don't want this to be happening whenever the camera is not working uh the camera Collision isn't uh supposed to work just as an example I'll just show here so it does work basically if you can see here see I'm colliding with nothing the camera here if you take a look there's nothing colliding it right so it's constantly setting it but thing is like I just don't want to use any processing power or any kind of calculations here if obviously we're just going to keep it with uh the default spring arm length so I just add a conditional branch check here and the same thing all of this calculation can just be uh ignored right so this is just for that and what we do here is is check if the spring on length is at its default length if it's nearly equal if uh we do nothing so if it's not at its Baseline uh Target arm length then we start uh setting it with the interpolation function we already have here this is pretty much the same as this except the target here is the default spring arm length and here it's the impact Point here and and then for the inter speed function here it's a pure fun uh function I created what we're doing here is getting the follow camera's location and the impact point and then we're going to get the distance between those two and then we're going to map range them and clamp them so what and and we're going to get the value of these two points vectors distance and uh we have a range eight to B an input range a Tob and an output range a Tob so in case some of you don't know what this does uh if you just hover over it the tool tip is uh predict self-explanatory Returns value maap from one range into another where the value is clamped to the input range uh example 0.5 normalized range normalized from the range 0 to 1 to 0 250 would result in 25 so basically if this uh let's say this target arm length is is it's set to zero right now let's say this is uh 500 and here it's uh let's say it's zero so 500 to uh 0 to 500 right so if it's 400 and this is uh 0 to one then it'll be spitting here it'll be spitting out a value of 0 four wait 0 500 and if it's 400 and this is zero to one 0 to 500 400 that's 80% so it'll be give spitting out 0.8 if I'm correct it's a bit confusing but basically uh I might have just said something weird but basically all you have to worry about is uh just these these tool tips you get the input value it sets a normalized range from A to B and then Maps it to A to B on the output and then it returns that to here so what I'm trying to achieve here is I want I don't want the inter speed we I mean basically you could just have it as a fixed value and it still works like like so if I just set it to like let's say just five you can see here it still works right nothing wrong with this but then this is just something uh I wanted to try implementing because if the camera here so this is an issue where the camera is trying to interpolate through the wall and if this distance is too far I wanted to I wanted the camera to kind of pull in as quickly as possible so it doesn't stay inside this clipping state for too long if it's a consistent if it's a constant value regardless of that that it might uh happen but then if we have like a custom function here where it uh Maps out the distance of the current camera to the impact Point uh we can map it into a certain in another output range then we can kind of create a system where if the distance is far we have a faster interpolation speed if the the camera here it's for example like this these two distance this es these two points uh distance is really uh small so the interpolation speed would be okay still if it's uh smaller so this part is just a function I created but you can take a look at it and if you can uh if you feel it unnecessary then uh you can just basically Lo uh use a constant value here or some other different value you want and that's it um however if you have a Target offset in this system it can cause an issue so right now as you can see uh and especially it goes up here it still has an issue so there are just some issues I wanted to address uh the which is first the target offset let's go with this first here if we set the Target off set to uh 500 oh no that's too much 100 the thing is uh it can come off and a so basically there the target offset isn't exactly the same as where is it oh the camera the camera's World location so even if if you add a spring Target offset here 100 you it doesn't automatically add the target offset here so you probably want to figure that out if you're trying to use a Target offset I think one method you could try is just kind of get the target offset here and then maybe add the variables here together and then may it it could work maybe I haven't tried that yet but I think you could experiment with that so that's one thing to consider so if you're doing if you follow this uh blueprint here and if it's working not really as intended maybe try checking if you have a Target offset value set and maybe uh uh try the method I just me uh mentioned here I think it might try to work I didn't have much time to work on this more so I have haven't been able to figure it out yet uh hopefully maybe if I have the time I can come back to this but I think as of now it's just uh getting the spring arms Target offset could work with that and then the other issue here is right now I don't have these uh objects here with any specific material function or anything so I'm just going to get rid of the deu it's getting a little visually disturbing so distracting I mean uh so yeah here so you can still kind of see it clipping through even though I have it uh in a way that if the distance to the impact point and the camera is Big it gets it it snaps it interpolates at a faster speed like so you can see that it still has has to clip through it and I think I mentioned in one of my previous videos I did that with the characters material with the dithering uh but for in this case I created a simple material for the World objects it does change up things a little bit so in terms of uh the art style in your game you might want to figure out a way to use something like this but this is is just for for you to get the idea even if it's clipping through if I have a dither which is like this if it's very close to the camera I have the material set so the camera if the camera is close to those materials it dther them like so then here and here so you can see it does provide a bit more visual uh Clarity to the player if you have some sort of material function to help you with it uh of course this system right now as of now it's not really perfect obviously but I would hope that it could help others like anyone watching this as a starting point to expand on their uh much cooler camera systems they're thinking of uh before we wrap up I'll just show a simple I think some some people might just wonder what I what I what I did with the materials nothing fancy I just get the world position and get the distance to the camera position then I remap it to uh uh value normalized range it's it's like the it's like this node basically map range clamps a ver of material and then you know inputs start distance and distance and then I get the threshold so because I wanted to normalize it from 0 to one right and this Alpha takes a value from 0 to one if my understanding is correct so I need something to get from 0 to one and I got the distance and if the distance is from if 70 it returns a zero 70 or below it returns a zero and if it's uh if it reaches 200 it uh it provide it spits out a results of one which will mean full dther well I mean the other way around but uh yeah you got the IDE so if you want you can just copy this absolute World position you can just search for this absolute well just World position Works get this node by searching for World position you can get this by camera position oops like this like so you can get this you know you can just search off distance search for we map value and these are just parameters I just you can just press one get a value here right click convert to parameter name them something and then uh to a and you can set it to a certain default value and then if you create a material instance you can change the value here oh but material isn't the main part of this video so that's just a quick look over it and like always I'm always uh looking forward to hearing any suggestions or feedback on my videos and my content thank you for watching the video um and yeah I hope you all have a great [Music] day
Info
Channel: gotagamee
Views: 17,076
Rating: undefined out of 5
Keywords:
Id: cFfCRpxl9c4
Channel Id: undefined
Length: 22min 59sec (1379 seconds)
Published: Mon Oct 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.