42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
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 void Awake()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
public void OnMove(InputValue value)
|
|
{
|
|
movementInput = value.Get<Vector2>();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
// 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);
|
|
|
|
// Rotate the boat
|
|
var turn = movementInput.x;
|
|
var turnRotation = Quaternion.Euler(0f, turn * turnSpeed, 0f);
|
|
rb.MoveRotation(rb.rotation * turnRotation);
|
|
}
|
|
}
|
|
} |