Swimming help

Hi guys! I’m trying to make a game with swimming mechanics but I need help with code. I have tried multiple tutorials but none work or are too hard. I would like the movement to be like Subnautica but everything I try doesn’t work. I am currently using the prefab in the asset store for a simple first person controller with a rigidbody but I am willing to change the movement if necessary. Thanks!

If a purpose-built tutorial complete with visuals and everything doesn’t work for you, I guarantee nothing in this little text box will either.

Perhaps you aren’t doing tutorials correctly?? That’s an extremely common problem.

Here’s the two-step solution:

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Most of the tutorials are not compatible with my unity version etc. I use Unity 6 but some are from older versions and I have tried repeated them and nothing really seems to work. I wouldn’t really want to downgrade too because that would mean some of the stuff I use will be out of date, thank you for the help though.

This usually means you’re trying tutorials with too much complexity.

Something like Subnautica is far far far from a beginner game.

Your first 10 games or so should be something on the order of Flappy Birds, Snake, Pong, Space Invaders, etc.

Swimming is simply a matter of disabling a rigidbody’s gravity when it enters a body of water. Some character controller scripts will only allow your character to move around when on the ground, but it’s easy to modify them so that they also allow your character to move when gravity is disabled.

The script on the water object could look something like this:

	void OnTriggerEnter(Collider other)
    {
		other.attachedRigidbody.useGravity=false;
    }

	void OnTriggerExit(Collider other)
	{
		other.attachedRigidbody.useGravity=true;
	}

There are a lot of different ways for swimming controls to work. I don’t remember exactly how the controls are in Subnautica, though. The way I’ve implemented swimming before is-

I have a free-look camera so you can look up/down/left/right with the mouse. The camera also follows the player with a delay. This is all easy to do with Cinemachine.

When you move the player with “wasd” it moves the player relative to the camera, (ie. the camer’s transform forward, and right) not the player’s.

I use addforce to move the player. This makes the movement very ease-in, ease-out. When you want to move it takes time to build-up speed. When you want to stop, it takes time to slow down. This makes the movement feel more like you’re in the water. Adjust the rigidbody’s drag accordingly.

While unity does change, the general jist does not, so, following a tutorial written since unity 2018 was a thing (eg after unity 5), that made enough sense to you

what actually and very specifically did not work? what was wrong? what didnt come up when you googled it? lots of tutorials get comments on how to make it work in later versions, so, unless youre grabbing an asset that hasnt been updated to unity 6 yet, you should be able to follow the tutorial and google the issues and get a result, so for us who cant see the tutorial (and dont overly want to watch it) and all that, explain in crayon if necessary, what isnt working and what you couldnt sort out with google

1 Like

Well lots of the tutorials we’re very old and from unity 5 and below but the ones which weren’t didn’t and even though they didn’t work they didn’t meet my requirements for what I needed in the game.

That is not how tutorials work. I’m sorry you were under that misconception.

Tutorials work when you a) do them perfectly, and b) take the time to understand 100% of the contents within them.

By “understand” I mean “know the subject matter well enough to explain to your grandmother what and why you are doing each part of the process.”

As far as “your requirements” go, that’s on you. You will NEVER find a tutorial that does precisely what you have in mind. That’s just not a thing.

There’s no meaningful difference between a swimming simulator and a flight simulator: you’re going forward and steering. In one case you hear bubbling water swishy noises, in the other you hear jet turbine howling and wind noise.

Steps to success:

  • learn how to put a script on a Camera
  • learn how to steer your transform up/down and left / right (see what @kdgalla said above)
  • learn how to make that script move the Transform forward when you press the swim button
  • learn how to make bubbling audio sounds in a loop

If you run into trouble doing this stuff, here is:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

Ok thank you I have tried my best but I think I’m gonna sort it out myself so I might try learning a bit more since I am quite a begimner but thanks

1 Like

We were all just like you once.

As I noted above, set yourself up for success by targeting a scope-appropriate game, such as the ultra-simple ones you see in various “make your first game” tutorials.

If you can’t rip out one of those ultra-simple games from the tutorial, trying to do something like Subnautica isn’t happening.

This is an awesome process: keep asking yourself “Can I …?”

Imphenzia: How Did I Learn To Make Games:

I am going to make my own custom script using add force and try and make it work myself but thanks for everyone’s help

Dont give up hope, but it is an important thing not to expect a tutorial to be an exact match to your requirements. as Kurt says, swimming is arguably just a method of movement, so if you can already move, its maybe a case of calling a different animation, or, if you dive, it becomes more like an airplane, but it is important not to get stuck in tutorial hell, where all you do is watch tutorials and try and mash them together, it is important to understand the meaning of the tutorial, not just blindly type out the code…

I enjoy watching tutorials to see if they use other methods to how I might do it, to potentially add other methods to my collection of ideas. Its rare I need the tutorial to do something Ive never tried, and one of the few times I did, l stopped following the tutorial having already rushed on and done much more than they were covering.

Tutorials should be a bit like looking things up in the dictionary, go get that one thing, the understanding of the word or the exact spelling, and move on.

1 Like