using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[Tooltip("Forward/back speed (units/sec).")]
public float speed = 1.89f;
[Tooltip("Turn speed (degree/sec).")]
public float rotationSpeed = 89.0f;
private Rigidbody rb;
private void Start ()
{
rb = GetComponent<Rigidbody>();
if (rb == null) Debug.LogWarning("PlayerController needs a Rigidbody.");
}
private void FixedUpdate()
{
Vector2 moveInput = Vector2.zero;
// Foreward/backward for the player
if (Keyboard.current.wKey.isPressed || Keyboard.current.upArrowKey.isPressed) moveInput.y = 1f;
if (Keyboard.current.sKey.isPressed || Keyboard.current.downArrowKey.isPressed) moveInput.y = -1f;
// Left/right (rotation) for the player
if (Keyboard.current.aKey.isPressed || Keyboard.current.leftArrowKey.isPressed) moveInput.x = -1f;
if (Keyboard.current.dKey.isPressed || Keyboard.current.rightArrowKey.isPressed) moveInput.x = 1f;
// Move is facing direction for our player
Vector3 movement = transform.forward * moveInput.y * speed * Time.fixedDeltaTime;
rb.MovePosition(rb.position + movement);
// Y-axis rotation (invert when going backwards.) For the Y Axis rotation of our player
float turnDirection = moveInput.x;
if (moveInput.y < 0)
turnDirection = -turnDirection;
float turn = turnDirection * rotationSpeed * Time.fixedDeltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
rb.MoveRotation(rb.rotation * turnRotation);
}
}