using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(PlayerInput))]
public class TwinStickMovement : MonoBehaviour
{
[SerializeField] private float playerSpeed = 5f;
[SerializeField] private float gravityValue = -9.81f;
[SerializeField] private float controllerDeadzone = 0.1f;
[SerializeField] private float gamepadRotateSmoothing = 1000f;
[SerializeField] private bool isGamepad;
private CharacterController controller;
private Vector2 movement;
private Vector2 aim;
private Vector3 playerVelocity;
private PlayerControlls playerControlls;
private PlayerInput playerInput;
private void Awake()
{
controller = GetComponent<CharacterController>();
playerControlls = new PlayerControlls();
playerInput = GetComponent<PlayerInput>();
}
private void OnEnable()
{
playerControlls.Enable();
}
private void OnDisable()
{
playerControlls.Disable();
}
// Update is called once per frame
void Update()
{
HandleInput();
HandleMovement();
HandleRotation();
}
void HandleInput()
{
movement = playerControlls.Controls.Movement.ReadValue<Vector2>();
aim = playerControlls.Controls.Aim.ReadValue<Vector2>();
}
void HandleMovement()
{
Vector3 move = new Vector3(movement.x, 0, movement.y);
controller.Move(move * Time.deltaTime * playerSpeed);
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
void HandleRotation()
{
if (isGamepad)
{
//Rotate our Player
if (Mathf.Abs(aim.x) > controllerDeadzone || Mathf.Abs(aim.y) > controllerDeadzone)
{
Vector3 playerDirection = Vector3.right * aim.x + Vector3.forward * aim.y;
if(playerDirection.sqrMagnitude > 0.0f)
{
Quaternion newrotation = Quaternion.LookRotation(playerDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, newrotation, gamepadRotateSmoothing * Time.deltaTime);
}
}
}
}
public void OnDeviceChange(PlayerInput pi)
{
isGamepad = pi.currentControlScheme.Equals("Gamepad") ? true : false;
}
}