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;
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
{
|
|
|
|
rb = GetComponent<Rigidbody>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnMove(InputValue value)
|
|
|
|
{
|
|
|
|
movementInput = value.Get<Vector2>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixedUpdate()
|
|
|
|
{
|
2023-08-03 02:54:21 +00:00
|
|
|
// Calculate the desired velocity
|
|
|
|
Vector3 desiredVelocity = transform.forward * movementInput.y * maxSpeed;
|
|
|
|
|
|
|
|
// If moving forward, use acceleration. Otherwise, use deceleration.
|
|
|
|
float speedChange = (movementInput.y != 0 ? acceleration : deceleration) * Time.fixedDeltaTime;
|
|
|
|
|
|
|
|
// Adjust the current velocity towards the desired velocity
|
|
|
|
rb.velocity = Vector3.MoveTowards(rb.velocity, desiredVelocity, speedChange);
|
2023-08-03 01:03:08 +00:00
|
|
|
|
|
|
|
// Rotate the boat
|
|
|
|
float turn = movementInput.x;
|
|
|
|
Quaternion turnRotation = Quaternion.Euler(0f, turn * turnSpeed, 0f);
|
|
|
|
rb.MoveRotation(rb.rotation * turnRotation);
|
|
|
|
}
|
|
|
|
}
|
2023-08-03 02:54:21 +00:00
|
|
|
}
|