- Home /
Make Player Swim in water
Hello. I was wondering whether it is possible to make the player float in water; the player is running on a race track and when the player touches a pool, the player will start to sink and will have to press the up button to float. I was wondering whether it is possible to do that and what kind of script would I have to use. Do I have to use a buoyancy script then link it to my running script? What is recommended to use in this case? Please advise! Thank you in advance! :)
How do you plan to control your character? Are you using the first or third-person controller? Did you create your own movement system?
The answer is yes, you can make a character float on in water, but the method for doing so differs on how you answer the above questions.
Hello! I'm planning on using the control buttons on the keypad to move the character; the left/right/up/down and space buttons. I'm using first-person controller. I'm using the script given in the default First Person Controller. Thank you for replying!
This is the movement script that I am using:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate()
{
if (grounded)
{
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// $$anonymous$$ove the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
function DoSomething()
{
speed = 6.0;
}
Answer by Chris D · Jul 05, 2011 at 02:49 AM
There is an excellently answered related question here. It's using a modified third-person controller, but the approach is transferable.
That being said, try to break down the components of your problem into the smallest logical pieces. You need to:
detect contact with the water
disable land-type movement and enable water movement
OR add onto land type movement with water movement also
Some things to consider include how or if you want to model buoyancy, how you want the density change of water to affect movement speed (if at all), etc. The easiest way to toggle between the two states is with the OnTriggerStay function.
Okay thank you so much! I will try it out and ask again if I have any problems. Thank you!
No problem, good luck!
Also, if this has answered your question to your satisfaction, please mark it as accepted (check mark next to thumbs up/down) so people know you found a solution.
Your answer
Follow this Question
Related Questions
Swimming/Underwater 3 Answers
How can I fake buoyancy (floating) with a configurable joint (or with a script)? 3 Answers
How to make a lake that you can swim in 2 Answers
Aquarium kinda thingy 1 Answer
I need help with water as a whole 1 Answer