OldBlueWater/BlueWater/Assets/02.Scripts/WaterAndShip/Player.cs

86 lines
2.2 KiB
C#
Raw Normal View History

2023-08-08 07:08:41 +00:00
using Cinemachine;
2023-08-03 01:03:08 +00:00
using UnityEngine;
using UnityEngine.InputSystem;
namespace _02.Scripts.WaterAndShip
{
public class Player : MonoBehaviour
{
2023-08-03 02:54:21 +00:00
public float maxSpeed = 10f;
public float acceleration = 2f;
public float deceleration = 2f;
2023-08-03 01:03:08 +00:00
public float turnSpeed = 10f;
private Rigidbody rb;
private Vector2 movementInput;
2023-08-08 07:08:41 +00:00
private bool isAssaultMode;
2023-08-03 01:03:08 +00:00
2023-08-04 16:02:49 +00:00
private void Awake()
2023-08-03 01:03:08 +00:00
{
rb = GetComponent<Rigidbody>();
}
2023-08-08 07:08:41 +00:00
private void FixedUpdate()
{
MovePlayer();
RotatePlayer();
}
#region AssaultMode/DreadgeMode Switch
public void OnModeChange(InputValue value)
{
if (isAssaultMode)
{
SwitchToDredgeMode();
}
else
{
SwitchToAssaultMode();
}
}
private void SwitchToDredgeMode()
{
GameManager.Inst.CameraController.CamDredgeMode();
isAssaultMode = false;
}
2023-08-03 01:03:08 +00:00
2023-08-08 07:08:41 +00:00
private void SwitchToAssaultMode()
{
GameManager.Inst.CameraController.CamAssaultMode();
isAssaultMode = true;
}
#endregion
#region Movement
2023-08-03 01:03:08 +00:00
public void OnMove(InputValue value)
{
movementInput = value.Get<Vector2>();
}
2023-08-08 07:08:41 +00:00
private void MovePlayer()
2023-08-03 01:03:08 +00:00
{
2023-08-03 02:54:21 +00:00
// Calculate the desired velocity
2023-08-04 16:02:49 +00:00
var desiredVelocity = transform.forward * movementInput.y * maxSpeed;
2023-08-08 07:08:41 +00:00
2023-08-03 02:54:21 +00:00
// If moving forward, use acceleration. Otherwise, use deceleration.
2023-08-04 16:02:49 +00:00
var speedChange = (movementInput.y != 0 ? acceleration : deceleration) * Time.fixedDeltaTime;
2023-08-08 07:08:41 +00:00
2023-08-03 02:54:21 +00:00
// Adjust the current velocity towards the desired velocity
rb.velocity = Vector3.MoveTowards(rb.velocity, desiredVelocity, speedChange);
2023-08-08 07:08:41 +00:00
}
2023-08-03 01:03:08 +00:00
2023-08-08 07:08:41 +00:00
private void RotatePlayer()
{
2023-08-03 01:03:08 +00:00
// Rotate the boat
2023-08-04 16:02:49 +00:00
var turn = movementInput.x;
var turnRotation = Quaternion.Euler(0f, turn * turnSpeed, 0f);
2023-08-03 01:03:08 +00:00
rb.MoveRotation(rb.rotation * turnRotation);
}
2023-08-08 07:08:41 +00:00
#endregion
2023-08-03 01:03:08 +00:00
}
2023-08-03 02:54:21 +00:00
}