OldBlueWater/BlueWater/Assets/02.Scripts/WaterAndShip/Player.cs
2023-08-08 16:08:41 +09:00

86 lines
2.2 KiB
C#

using Cinemachine;
using UnityEngine;
using UnityEngine.InputSystem;
namespace _02.Scripts.WaterAndShip
{
public class Player : MonoBehaviour
{
public float maxSpeed = 10f;
public float acceleration = 2f;
public float deceleration = 2f;
public float turnSpeed = 10f;
private Rigidbody rb;
private Vector2 movementInput;
private bool isAssaultMode;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
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;
}
private void SwitchToAssaultMode()
{
GameManager.Inst.CameraController.CamAssaultMode();
isAssaultMode = true;
}
#endregion
#region Movement
public void OnMove(InputValue value)
{
movementInput = value.Get<Vector2>();
}
private void MovePlayer()
{
// Calculate the desired velocity
var desiredVelocity = transform.forward * movementInput.y * maxSpeed;
// If moving forward, use acceleration. Otherwise, use deceleration.
var speedChange = (movementInput.y != 0 ? acceleration : deceleration) * Time.fixedDeltaTime;
// Adjust the current velocity towards the desired velocity
rb.velocity = Vector3.MoveTowards(rb.velocity, desiredVelocity, speedChange);
}
private void RotatePlayer()
{
// Rotate the boat
var turn = movementInput.x;
var turnRotation = Quaternion.Euler(0f, turn * turnSpeed, 0f);
rb.MoveRotation(rb.rotation * turnRotation);
}
#endregion
}
}