init
This commit is contained in:
commit
001bb14f16
951 changed files with 270074 additions and 0 deletions
44
Assets/Scripts/New/PlayerMovement.cs
Normal file
44
Assets/Scripts/New/PlayerMovement.cs
Normal file
|
@ -0,0 +1,44 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue