Untitled
unknown
csharp
2 years ago
1.3 kB
4
Indexable
Never
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(); Rotate(); } void Rotate() { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(inputDirection), rotateSpeed * Time.deltaTime); } }