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 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.0f;
public float groundDistance = 0.4f;
public float jumpHeight = 3f;
public float riseSpeed = 6f;
public float swimSpeed = 6f;
public LayerMask groundMask;
public LayerMask waterMask;
public Transform groundCheck;
public Transform waterCheck;
Vector3 velocity;
bool isGrounded;
bool isSwimming;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
isSwimming = Physics.CheckSphere(waterCheck.position, groundDistance, waterMask);
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
if (isSwimming)
DoSwim();
else
DoWalk();
void DoSwim()
{
Vector3 swim = transform.right * x + transform.forward * z; //Vector3 swim = transform.right * x + transform.forward * z;
controller.Move(swim * swimSpeed * Time.deltaTime);
}
void DoWalk()
{
// isGrounded = Make a sphere at (groundCheck object's position, groundDistance for the value of the radius, groundMask for the Layermask)
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
// If isGrounded is true & velocity < 0
if (isGrounded && velocity.y < 0)
{
// Set vertical velocity to zero
velocity.y = 0f;
}
// ---
Vector3 move = transform.right * x + transform.forward * z;
// Move player as move * speed value * Time.deltaTime for fps increase / decrease
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
// Velocity = Jump height - 2 * gravity (Jumping physics calculation)
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
}
Player Look Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public LayerMask groundMask;
public LayerMask waterMask;
public Transform groundCheck;
public Transform waterCheck;
public float groundDistance = 0.4f;
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
float yRotation = 0f;
bool isSwimming;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
isSwimming = Physics.CheckSphere(waterCheck.position, groundDistance, waterMask);
if (isSwimming)
DoSwim();
else
DoWalk();
void DoSwim()
{
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);
transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
playerBody.Rotate(Vector3.up * mouseX);
playerBody.Rotate(playerBody.rotation.x, 0, playerBody.rotation.z);
}
void DoWalk()
{
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);
transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
playerBody.Rotate(Vector3.up * mouseX);
playerBody.Rotate(playerBody.rotation.x, 0, playerBody.rotation.z);
}
}
}
Very thanks if you help ^^
