mirror of
https://github.com/ConjureETS/Bomberman.git
synced 2026-03-24 10:20:58 +00:00
61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
[RequireComponent(typeof(Rigidbody))]
|
|
[RequireComponent(typeof(PlayerInput))]
|
|
public class PlayerMovement : MonoBehaviour {
|
|
public PlayerData data;
|
|
|
|
new Rigidbody rigidbody;
|
|
Vector2 moveInput;
|
|
Vector3 moveDirection;
|
|
Transform camTransform;
|
|
|
|
void Awake() {
|
|
rigidbody = GetComponent<Rigidbody>();
|
|
|
|
var playerInput = GetComponent<PlayerInput>();
|
|
|
|
playerInput.actions["Move"].started += OnMove;
|
|
playerInput.actions["Move"].performed += OnMove;
|
|
playerInput.actions["Move"].canceled += OnMove;
|
|
|
|
if (Camera.main != null)
|
|
camTransform = Camera.main.transform;
|
|
else
|
|
Debug.LogError("Couldn't find the main camera.");
|
|
}
|
|
|
|
void Update() {
|
|
Vector3 camForward = camTransform.forward;
|
|
camForward.y = 0;
|
|
camForward.Normalize();
|
|
|
|
moveDirection = camForward * moveInput.y + camTransform.right * moveInput.x;
|
|
}
|
|
|
|
void FixedUpdate() {
|
|
if (moveDirection != Vector3.zero) {
|
|
rigidbody.MovePosition(rigidbody.position + moveDirection * data.speed * Time.fixedDeltaTime);
|
|
|
|
AlignGroundedRotation();
|
|
}
|
|
}
|
|
|
|
void AlignGroundedRotation() {
|
|
Quaternion goalRot = Quaternion.LookRotation(moveDirection);
|
|
Quaternion slerp = Quaternion.Slerp(transform.rotation, goalRot, data.turnSpeed * moveDirection.magnitude * Time.fixedDeltaTime);
|
|
|
|
transform.rotation = slerp;
|
|
}
|
|
|
|
void OnMove(InputAction.CallbackContext ctx) {
|
|
Vector2 input = ctx.ReadValue<Vector2>();
|
|
if (input.sqrMagnitude > 1f)
|
|
input.Normalize();
|
|
|
|
moveInput = input;
|
|
}
|
|
} |