twinStick/twinStickCrawler/Assets/Scripts/Creatures/Player/Player.cs

110 lines
3.6 KiB
C#
Raw Normal View History

using System.Collections;
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("Setup")]
[SerializeField]
private InputManager _inputManager;
[SerializeField]
private GameObject _cameraGameObject;
[SerializeField]
private Animator _animator;
[Header("Camera Options")]
[SerializeField] private Vector3 camOffset;
[SerializeField] private float cameraFollowSpeed = 2.5f;
[Header("Player Options")]
[Range(0f, 10f)]
public float moveSpeed = 5f;
[Range(0f, 100f)]
public float animationSpeed = 30f;
[Header("Enemy Snapping")]
[SerializeField]
private float minAimDistance;
[SerializeField]
private float maxAimDistance;
2024-12-23 17:13:28 +00:00
[SerializeField]
private Canvas aimCanvas;
[Header("Dash")]
[Range(0f, 100f)]
public float dashSpeed = 7f;
[Range(0f, 1f)]
public float dashTime = 0.2f;
[Range(0f, 4f)]
public float dashCooldownTime = 1f;
[SerializeField]
public TrailRenderer dashTrail;
[Header("Attacking")]
public LayerMask enemyLayer;
2024-12-23 17:13:28 +00:00
public float attackResetTime;
private Camera _camera;
private CharacterController _controller;
private Animate _animate;
private PlayerMove _playerMove;
private PlayerRotate _playerRotate;
2024-12-23 17:13:28 +00:00
private PlayerAimRotate _playerAimRotate;
private PlayerAttack _playerAttack;
private CameraMove _cameraMove;
// Start is called once before the first execution of Update after the MonoBehaviour is created
2024-12-23 17:13:28 +00:00
void Awake()
{
camOffset.z = camOffset.x;
_camera = _cameraGameObject.GetComponent<Camera>();
_controller = GetComponent<CharacterController>();
_playerRotate = gameObject.AddComponent<PlayerRotate>();
2024-12-20 14:21:20 +00:00
2024-12-23 17:13:28 +00:00
_playerAimRotate = gameObject.AddComponent<PlayerAimRotate>();
_playerAimRotate.initialize(aimCanvas);
_animate = _animator.gameObject.AddComponent<Animate>();
2024-12-20 14:21:20 +00:00
_animate.initialize(_animator);
_playerAttack = gameObject.AddComponent<PlayerAttack>();
2024-12-23 17:13:28 +00:00
_playerAttack.initialize(_inputManager, enemyLayer, _animate, attackResetTime);
_playerMove = gameObject.AddComponent<PlayerMove>();
_playerMove.initialize(_controller, dashTrail);
_cameraMove = gameObject.AddComponent<CameraMove>();
_cameraMove.initialize(transform, camOffset, cameraFollowSpeed, _camera.transform);
}
void Update()
{
Vector3 moveInput = new Vector3(_inputManager.moveInput.x, 0f , _inputManager.moveInput.y);
2024-12-20 15:51:03 +00:00
Vector3 moveDirection = _playerMove.GetMoveDirection(moveInput, _camera.transform);
2024-12-20 15:51:03 +00:00
Vector3 lookTarget = _playerRotate.LookTarget(moveDirection);
moveDirection += _playerMove.Gravity();
2024-12-20 15:51:03 +00:00
_playerMove.Dash(dashTime, dashCooldownTime, dashSpeed, _inputManager.sprintPerformed);
_playerMove.ApplyMovement(moveDirection, lookTarget, moveSpeed);
_cameraMove.MoveCamera();
2024-12-23 17:13:28 +00:00
Vector3 aimTarget = _playerAimRotate.AimTarget(minAimDistance, maxAimDistance, _camera, _inputManager.aimInput);
_playerAimRotate.ApplyRotation(aimTarget);
_playerAttack.Attack();
2024-12-20 15:51:03 +00:00
_animate.AnimateState("Walk", moveInput.magnitude, animationSpeed);
/*
//360 Char Animation
//Vector3 localMoveDirection = transform.InverseTransformDirection(moveDirection);
//_animate.AnimateState("X", localMoveDirection.x, animationSpeed);
//_animate.AnimateState("Y", localMoveDirection.z, animationSpeed);
2024-12-20 14:21:20 +00:00
2024-12-23 17:13:28 +00:00
//
2024-12-20 15:51:03 +00:00
*/
}
}