44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class PlayerMovement : MonoBehaviour{
|
|
|
|
CharacterController controller;
|
|
|
|
[SerializeField, Range(0f, 1f)] float inputDeadZone = .125f;
|
|
Vector2 playerInput;
|
|
|
|
[SerializeField] float speed = 5f;
|
|
|
|
Transform cam;
|
|
float turnVelocity;
|
|
|
|
// Start is called before the first frame update
|
|
void Awake(){
|
|
cam = Camera.main.transform;
|
|
controller = GetComponent<CharacterController>();
|
|
}
|
|
|
|
public void PlayerMove(InputAction.CallbackContext state){
|
|
Vector2 input = state.ReadValue<Vector2>();
|
|
if(input.magnitude > inputDeadZone){
|
|
playerInput = input;
|
|
}else{
|
|
playerInput = Vector2.zero;
|
|
}
|
|
playerInput = Vector2.ClampMagnitude(playerInput, 1);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update(){
|
|
if(playerInput.magnitude > inputDeadZone){
|
|
float targetAngle = Mathf.Atan2(playerInput.normalized.x, playerInput.normalized.y) * Mathf.Rad2Deg + cam.eulerAngles.y;
|
|
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnVelocity, .1f);
|
|
transform.rotation = Quaternion.Euler(0f, angle, 0f);
|
|
|
|
controller.Move(new Vector3(playerInput.x, 0f, playerInput.y) * speed * Time.deltaTime);
|
|
}
|
|
}
|
|
}
|