Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Swimming system

Discussion in 'Scripting' started by bora155, Feb 21, 2021.

  1. bora155

    bora155

    Joined:
    Mar 31, 2020
    Posts:
    12
    Hello guys . Im tryna implement swimming whenever i get in water . This is my code now . Any way how i can implement swimming?

    Player Movement Script :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     public CharacterController controller;
    9.  
    10.     public float speed = 12f;
    11.     public float gravity = -9.0f;
    12.     public float groundDistance = 0.4f;
    13.     public float jumpHeight = 3f;
    14.     public float riseSpeed = 6f;
    15.     public float swimSpeed = 6f;
    16.     public LayerMask groundMask;
    17.     public LayerMask waterMask;
    18.  
    19.     public Transform groundCheck;
    20.     public Transform waterCheck;
    21.  
    22.  
    23.     Vector3 velocity;
    24.  
    25.     bool isGrounded;
    26.     bool isSwimming;
    27.  
    28.     // Start is called before the first frame update
    29.     void Start()
    30.     {
    31.        
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.         isSwimming = Physics.CheckSphere(waterCheck.position, groundDistance, waterMask);
    38.  
    39.         float x = Input.GetAxis("Horizontal");
    40.         float z = Input.GetAxis("Vertical");
    41.  
    42.         if (isSwimming)
    43.             DoSwim();
    44.         else
    45.             DoWalk();
    46.  
    47.         void DoSwim()
    48.         {
    49.             Vector3 swim = transform.right * x + transform.forward * z; //Vector3 swim = transform.right * x + transform.forward * z;
    50.             controller.Move(swim * swimSpeed * Time.deltaTime);
    51.         }
    52.  
    53.         void DoWalk()
    54.         {
    55.             // isGrounded = Make a sphere at (groundCheck object's position, groundDistance for the value of the radius, groundMask for the Layermask)
    56.             isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    57.  
    58.             // If isGrounded is true & velocity < 0
    59.             if (isGrounded && velocity.y < 0)
    60.             {
    61.                 // Set vertical velocity to zero
    62.                 velocity.y = 0f;
    63.             }
    64.  
    65.             // ---
    66.             Vector3 move = transform.right * x + transform.forward * z;
    67.  
    68.             // Move player as move * speed value * Time.deltaTime for fps increase / decrease
    69.             controller.Move(move * speed * Time.deltaTime);
    70.  
    71.             if (Input.GetButtonDown("Jump") && isGrounded)
    72.             {
    73.                 // Velocity = Jump height - 2 * gravity (Jumping physics calculation)
    74.                 velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    75.             }
    76.  
    77.             velocity.y += gravity * Time.deltaTime;
    78.  
    79.             controller.Move(velocity * Time.deltaTime);
    80.         }
    81.        
    82.     }
    83. }
    84.  
    85. Player Look Script
    86.  
    87.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseLook : MonoBehaviour
    6. {
    7.     public LayerMask groundMask;
    8.     public LayerMask waterMask;
    9.  
    10.     public Transform groundCheck;
    11.     public Transform waterCheck;
    12.  
    13.     public float groundDistance = 0.4f;
    14.  
    15.     public float mouseSensitivity = 100f;
    16.  
    17.     public Transform playerBody;
    18.  
    19.     float xRotation = 0f;
    20.     float yRotation = 0f;
    21.  
    22.     bool isSwimming;
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.         Cursor.lockState = CursorLockMode.Locked;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.  
    34.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    35.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    36.  
    37.         isSwimming = Physics.CheckSphere(waterCheck.position, groundDistance, waterMask);
    38.  
    39.         if (isSwimming)
    40.             DoSwim();
    41.         else
    42.             DoWalk();
    43.  
    44.         void DoSwim()
    45.         {
    46.             xRotation -= mouseY;
    47.             xRotation = Mathf.Clamp(xRotation, -90, 90);
    48.  
    49.             transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
    50.             playerBody.Rotate(Vector3.up * mouseX);
    51.             playerBody.Rotate(playerBody.rotation.x, 0, playerBody.rotation.z);
    52.         }
    53.  
    54.         void DoWalk()
    55.         {
    56.  
    57.             xRotation -= mouseY;
    58.             xRotation = Mathf.Clamp(xRotation, -90, 90);
    59.  
    60.             transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
    61.             playerBody.Rotate(Vector3.up * mouseX);
    62.             playerBody.Rotate(playerBody.rotation.x, 0, playerBody.rotation.z);
    63.         }
    64.  
    65.     }
    66. }
    67.  
    Very thanks if you help ^^
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    It looks like you already did implement swimming. I see a
    DoSwim()
    method in that code. What doesn't work about your current approach?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    I would make a walk controller you're happy with.

    Now make a swim controller you're happy with, completely separate.

    Now put them on the same player script and enable/disable them based on whether you want to swim or walk.

    That's how I would do it.

    Or else do some Youtube swimming tutorials.

    Screen Shot 2021-02-21 at 2.22.54 PM.png
     
  4. bora155

    bora155

    Joined:
    Mar 31, 2020
    Posts:
    12
    Hi everyone , i made a new post would be happy if you checked that out