Help With Swimming movement script.

I was wondering if someone could help me with a script the would allow me to swim underwater to were i could hold w and just move the mouse to mainly swim but the other keys move to, ad left right and s to move backward, but no button to go up, just move the mouse up to go up. i have a script were i can look with the mouse and move with wasd keys, both are attached.

CharacterControls

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterControl : MonoBehaviour {

    public float speed = 10.0F;

    // Use this for initialization
    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    //Update is called once per frame
    private void Update()
    {
        float translation = Input.GetAxis("Vertical") * speed;
        float straffe = Input.GetAxis("Horizontal") * speed;
        translation *= Time.deltaTime;
        straffe *= Time.deltaTime;

        transform.Translate(straffe, 0, translation);

        if (Input.GetKeyDown("escape"))
            Cursor.lockState = CursorLockMode.None;
    }

}

CamMouseLook

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class camMouseLook : MonoBehaviour {

    Vector2 mouseLook;
    Vector2 smoothV;
    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    GameObject character;

    void Start()
    {
        character = this.transform.parent.gameObject;
    }

    void Update()
    {
        var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

        md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
        smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
        smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
        mouseLook += smoothV;
        mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);

        transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
        character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);

    }

}

3037963–227400–camMouseLook.cs (1004 Bytes)
3037963–227401–CharacterControl.cs (733 Bytes)

I didn’t look them over (yet), but I can say with some certainty that people are more likely to respond if they can look at the code here in the forum post directly.
If you post your code (using tags!) people can look at it, and respond/comment much more easily.

I posted the code in the actual question do you think you could help me? Ive been trying to do this for the past week.

1 Like

Okay, with your code, I am moving side to side and “in / out” with the control keys. I didn’t modify any of that, but added a small bit that allows the mouse to swim up (it also goes down).

 //Update is called once per frame
    private void Update()
    {
        float translation = Input.GetAxis("Vertical") * speed;
        float straffe = Input.GetAxis("Horizontal") * speed;
      
        // adjust the speed accordingly. with the modification , it was super fast for me.
        // notice there is no limit on how far you can go :)
        float swimY = Input.GetAxis("Mouse Y") * speed * .05f;
        translation *= Time.deltaTime;
        straffe *= Time.deltaTime;

        transform.Translate(straffe, swimY, translation);

        if (Input.GetKeyDown("escape"))
            Cursor.lockState = CursorLockMode.None;
    }

Try that and work with it/play around a while… and see what ya like/what ya still need.
Let me know how it goes :slight_smile:

1 Like

I added your code to the CharacterControl but when i went in the water i fell to the floor and i couldnt actually float or go up. So do i need to make the water have like no gravity?water is at the position of y = 21.4.

Oh, sorry I never tested with gravity I guess. Oops. I didn’t think about that… figured if you’re in the water, you’d have a way to stay afloat and/or go down. I thought you already had that working, sorry. Quick test shows that when I put a rigidbody on (my cube) with a mass of 1 and drag of 100 it wasn’t falling anymore.
Maybe you can set the drag up high like that in the water.
as for floating, that’s another matter all together. I guess the way it is with the cursor lock it’ll float unless you move the cursor. Sorry let me know if that’s close at all for now