How to calculate Z location of object relative to the x/y location of player

My aim is to get swimming locked to the top of the water.

So far i have got it locked to Z as i enter the water. but this only works correctly with level water sources. i have a sloped river so this solution causes me to swim out of the water as i swim down stream and under it as i swim upstream.

the solution i have thought of is to calculate the z location of the water relative to the players current position. I am quite new to unreal so any help on how to achieve this would be great!

How did you make the river? Is it using a spline? If so you can sample the location on the spline that’s close to your player and use the Z component of that location.

If not using a spline, create an actor, add a spline component, add the actor to the level, modify the spline to match the river, then like above, sample the location closest to player and use the Z value to clamp player location.

imageimageimage


Place the actor with the spline in the level

Shape this spline so that it matches the river


Create a function inside the spline actor to sample the water height.

1 Like

yes i am using a spline.

Thank you for this, it sounds like the perfect solution. i will give it a go soon and let you know how i get on!

1 Like

hey one question about this set up.

When i start the WaterZ location is 0.0 untill i am submerged which results in me walking too far into the water on the first time i enter it. after that its fine as it correctly reads the location.

do you have any ideas on what could be causing this? or a way to get it to read correctly on loading so i get the level before entering the water?

nevermind, changing the tick time fixed the issue of walking underwater for a bit.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.