Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
1.6 kB
4
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PushDirection : MonoBehaviour
{
    public Camera mainCamera;
    public Vector3 inputDirection;
    public Vector3 lastDirection;

    public KeyboardControl keyboardControl;
    public InputAction Move;

    public float rotateSpeed = 2.5f;

    private void Awake()
    {
        keyboardControl = new KeyboardControl();

        mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
    }

        private void OnEnable()
        {
        keyboardControl.Enable();
        Move = keyboardControl.Keyboard.Move;

        Move.Enable();
        }

    private void OnDisable()
    {
        keyboardControl.Disable();
        Move.Disable();
    }

    private void Update()
    {
        inputDirection = mainCamera.transform.forward * Move.ReadValue<Vector2>().y + mainCamera.transform.right * Move.ReadValue<Vector2>().x;
        inputDirection.y = 0;
        inputDirection.Normalize();

        if (Move.ReadValue<Vector2>().x != 0 || Move.ReadValue<Vector2>().y != 0) //Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0
        {
            lastDirection = mainCamera.transform.InverseTransformDirection(inputDirection);
        }

        lastDirection.Normalize();
        Rotate();
    }

    void Rotate()
    {
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lastDirection), rotateSpeed * Time.deltaTime);
    }
}