Underwater Swimming

Hi,

I am currently using the standard Unity first person controller and I am trying to simulate underwater swimming. I understand that I need to set the first person controller movement to the cameras rotation to simulate the ability to ‘swim’ (basically flying in the direction of the camera).


The script I have so far semi-works. It checks that i am under water and then when I press the directional keys it will move in the direction I am facing the camera, but the issue is it does this very slowly.


For example if I enter the water and point the camera directly down and press W, the player will move forwards along the water plane and will eventually move ‘down’ but does not go directly to the forward position I am pointing. I think I am missing something rather basic and I have thought perhaps I need to set the rotation directly but I am struggling to find the correct solution for this. Any help or pointers is appreciated!

if (Input.GetKey(KeyCode.W)) {

			transform.Translate(Vector3.forward * Time.deltaTime * underwaterSpeedForward, Camera.main.transform);

		}

		if (Input.GetKey(KeyCode.S)) {

			transform.Translate(-Vector3.forward * Time.deltaTime * underwaterSpeedBack, Camera.main.transform);

		}

		if (Input.GetKey(KeyCode.D)) {

			transform.Translate(Vector3.right * Time.deltaTime * underwaterSpeedStrafe, Camera.main.transform);

		}

		if (Input.GetKey (KeyCode.A)) {

			transform.Translate (-Vector3.right * Time.deltaTime * underwaterSpeedStrafe, Camera.main.transform);

		} 

		if (Input.GetKey (KeyCode.Space)) {

			transform.Translate (Vector3.up * Time.deltaTime * upSpeed);

		}

Is the full script like this? `using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
private float underwaterSpeedForward = 10f;
public float underwaterSpeedBack = 7f;
public float underwaterSpeedStrafe = 7f;
public float upSpeed = 7f;

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if (Input.GetKey(KeyCode.W))
    {
        transform.Translate(Vector3.forward * Time.deltaTime * underwaterSpeedForward, Camera.main.transform);
    }
    if (Input.GetKey(KeyCode.S))
    {
        transform.Translate(-Vector3.forward * Time.deltaTime * underwaterSpeedBack, Camera.main.transform);
    }
    if (Input.GetKey(KeyCode.D))
    {
        transform.Translate(Vector3.right * Time.deltaTime * underwaterSpeedStrafe, Camera.main.transform);
    }
    if (Input.GetKey(KeyCode.A))
    {
        transform.Translate(-Vector3.right * Time.deltaTime * underwaterSpeedStrafe, Camera.main.transform);
    }
    if (Input.GetKey(KeyCode.Space))
    {
        transform.Translate(Vector3.up * Time.deltaTime * upSpeed);
    }
}

}

`

This tutorial gives a simple approach to swimming in Unity