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);
}
Should you use transform.forward, transform.right, and transform.up ins$$anonymous$$d of Vector3 versions of these?
Also, you could multiple the direction vectors with a swimSpeed variable.
Answer by theolympicreefback · Jun 05, 2020 at 01:29 PM
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); } } }
Your answer
Follow this Question
Related Questions
SmoothCameraCrouch 2 Answers
How to make an object go the direction it is facing? 0 Answers
How to Parent a Camera to the Head Bone with No Camera Shake? 0 Answers
Sit Down First person 0 Answers
Camera Tracking 1 Answer