Some issues with swimming

Hey, I am making “swimmable” water I guees you could say, and I am having a few setbacks. Basically my water is a flat plane with a box collider that reaches all the way down to the terrain. I have another collider where my player’s main camera is, and when those colliders trigger, the render settings are changed so it looks like you are under water. When the collider exit, render settings are changed back to normal.

So, I can jump in the water, and it LOOKS like I’m swimming. Yay for that. But I want to implement some swimming physics, so that you aren’t walking on the bottom of an endless ocean all the time :). Here is the details:

  1. My player is using character controller with an edited First Person Control script.
  2. I normally just use “simpleMove” to move forward, back, left, and right.
  3. I have a public boolean (yes I know how to access it in another script) called isSwimming, and when it is true, I want all the swimming physics to be true.
  4. I want gravity to be slightly negative when isSwimming = true, so that it is like you are floating up slowly.
  5. Instead of moving in vector3 direction like on land, I want to move where the CAMERA IS LOOKING when in water. This is the biggest issue I am struggling with. So that when you look up, the controller moves up. and down, down.

I know this is a big question, but I really need help with this. I can’t for the life of me figure out how to edit Character Controller gravity, and I have no idea how to find out where the camera is looking.

Bump. Any ideas?

Use CharacterController.Move() so you can apply gravity:

public float moveSpeed = 5; // units/second
public float rotateSpeed = 90; // degrees/second
public float jumpSpeed = 5; // units/second

private float gravity = -9.8f;
private float verticalVelocity = 0;
private CharacterController controller;

void Awake() {
    controller = GetComponent<CharacterController>();
}

void Update() {
    transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
    var velocity = transform.forward * Input.GetAxis("Vertical") * moveSpeed;
    if (controller.isGrounded) {
        verticalVelocity = Input.GetKeyDown("space") ? jumpSpeed : 0;
    }
    verticalVelocity += gravity * Time.deltaTime;
    velocity.y = verticalVelocity;
    controller.Move(velocity * Time.deltaTime);
}

void OnTriggerEnter(Collider other) {
    if (other.CompareTag("Water")) gravity = 1;
}

void OnTriggerExit(Collider other) {
    if (other.CompareTag("Water")) gravity = -9.8f;
}

I just typed this directly into the reply. It might have bugs or typos, but it should convey the general idea.

Ahh, so that is how I can change gravity!

but you know how I can find the direction the camera is facing and rotate the player capsule to that? Because right now, I can only move Sideways, backwards, and forward when isGrounded, so I can’t move when in water lol xD

Thanks!

Try using the First Person Controller prefab in Standard Assets or the new one in the Sample Assets (Beta) on the Asset Store.

Well, I am using a “modified” version of that script. However, I that script basically tells the capsule/player to stay completely vertical all the time, and it is ONLY able to move, left, right, backwards, and sideways. Than the camera pivots around the players y/x axis.

Actually, now you have me wondering. Let me go make sure I am right about this. I will update this reply with my findings.

EDIT: Ya I am kinda right. Except the CAPSULE rotates on y axis, and the CAMERA rotates on x axis. So, the capsule doesnt actually face or move where the camera is pointing.

EDIT2LOL: Ok, so I kinda changed up my swim feature. So now when you hit the water, gravity is disabled. But two buttons appear, if you hit the bottom one, gravity = -4, and if you hit the top one gravity = 4. This allows you to swim up and down while still being able to look around. It is actually alot of fun :p. And thanks for the help, I really needed to know how to change gravity.

Happy to help!