- Home /
Adding Swimming and diving to character motor?
Hey working on a player input system to my game I've made a huge remake of the motor coming with unity3D, but over all it does the same but adding a few needs i had and fixing the sliding/gravity/jumping problem but one thing it does not add is a swimming/diving script and i've been looking a lot around for some way to do the clean and easy... so far a lot of people seems to use triggers that i believe is an okay solution for SWIMMING but yet i haven't found any thing adding a smooth diving script.
My idea is to maybe allow the character to diving when isswimming = true only, however i find it a little silly to use triggers since there would be MANY places with water... So if there was an easier/other way of doing it i would prefer that. When there is no need to have an "float up" force, however it would be neat to have. An other thing is i would prefer not to use a rigid-body on my characters is possible.
Since diving a flying is almost the same i bet i could use a dive script for flying too maybe???
So resume: What would you add to the character motor to make a smooth swimming and diving option to the character(s)? (and trying avoid using triggers/rigid-body)
My own result and effort so far: I've made a class for all the swimming and diving properties but ignoring gravity is mocking me at time and i suddenly fall the the bottom when diving using the triggers...
Thanks Jackie
Answer by wolventoad · Jul 30, 2012 at 01:04 AM
I prefer Trigger Volume, maybe you can try OnTriggerStay(), and don't make all of the water have trigger volumes, only the water you need to dive and swim, or use tag to seperate them. But as you don't like it, I'm just saying it.
Another way is force calculate the coordinates, get the CharacterController's position, height and weight. Then calculate them to check if they are in the water you need to swim/dive.
There gonna be more ways to do it. These are just two I use often. Good luck.
Thanks for the answer, im not 100% sure i get what you mean but atm all water should/are planed to be swimable, and i wanna avoid triggers because in my current code it seems to make thing worse, and also if there should be like 100+ water pools adding all component gonna be a hell... i was thinking about if raycast was an option but still no luck for me... Swim$$anonymous$$g is no problem but diving is the bitch in the script...
ps i really appreciate sample codes since that many times start ideas how is should do.
Hmm, let me guess.
Your swim has no problem. Just need to check which water's depth is fit to dive, right?
i guess yes, idk? need to know then i look down (FPS camera) it will dive in that direction you're facing but when you're swim$$anonymous$$g you should only be able to either swim straight one way or dive, and free movement then diving etc.
Try your current way, Raycast.
Use Raycast to check if this is the water you canDive. You can search the official docs, there should be a magnitude function to check the length of the ray.
if canDive == true, when your character's eulerAngle is lower than a certain limit, you can make him to go under the water and move around. And change isDiving = true;
if isDiving, once you move above the water, change isDiving to false, and return to swim status.
I hope this idea can help you.
i'll give it a try, would i still need a trigger for swim or could i do this with a raycast too?
Answer by murkantilism · Jul 30, 2012 at 03:39 AM
This tutorial on implementing player swimming might be helpful to you: Click me!
And if you're curious the full tutorial series can be found here: Click me!
In my opinion making swimming/diving smooth will mostly be an issue of good animation. I think a simple boolean like you suggested would be fine, or using a trigger system in front of your bodies of water would work just as well.
already watched i wanna avoid doing what he does and swim$$anonymous$$g is the least of my problem its diving that is the main problem
I have a diving and surfacing feature at this Unity Answers page, but there's a problem with the character continuing to dive and surface when you let your finger off of a button. However, if you dive, then press the jump/surface button, you can still stabilize yourself in the water and/or surface. It also works vice-versa.
Your answer
Follow this Question
Related Questions
CharacterMotor Gravity Set 1 Answer
[HELP] Swim/dive script 0 Answers
C# CharacterMotor crashing. What am I doing wrong? 1 Answer
Making a character move automatically in one direction. 2 Answers