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

174 lines
4.6 KiB
C#
Raw Normal View History

2024-12-17 21:46:05 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
public class PlayerMove : MonoBehaviour
{
[Header("Setup")]
[SerializeField]
private InputManager _inputManager;
[SerializeField]
private GameObject _cameraGameObject;
[Header("Player Options")]
[Range(0f, 10f)]
public float moveSpeed = 5f;
[Header("Enemy Snapping")]
[SerializeField]
private float minAimDistance;
[SerializeField]
private float maxAimDistance;
[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;
private Camera _camera;
private CharacterController _controller;
private bool _dashing = false;
private bool _dashPossible = true;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
_camera = _cameraGameObject.GetComponent<Camera>();
_controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Vector3 input = new Vector3(_inputManager.moveInput.x, 0f , _inputManager.moveInput.y);
Vector3 lookTarget = LookTarget();
Vector3 moveDir = Vector3.zero;
moveDir += MoveDir(input);
moveDir += Gravity();
Dash();
ApplyMovement(moveDir, lookTarget);
}
#region basicMovement
void ApplyMovement(Vector3 moveDir, Vector3 lookTarget)
{
if (!_dashing)
{
transform.LookAt(lookTarget);
_controller.Move(moveSpeed * moveDir * Time.deltaTime);
}
}
Vector3 MoveDir(Vector3 input)
{
if (_dashing)
{
return Vector3.zero;
}
Quaternion camRotation = Quaternion.Euler(0f, _cameraGameObject.transform.rotation.eulerAngles.y, 0f);
input = camRotation * input;
return input;
}
Vector3 Gravity()
{
return Physics.gravity;
}
Vector3 _lastLookTarget;
Vector3 LookTarget()
{
if (_dashing)
{
return _lastLookTarget;
}
Ray ray = _camera.ScreenPointToRay(Input.mousePosition); // Cast a ray from the mouse position
Plane plane = new Plane(Vector3.up, transform.position); // Assume a horizontal plane (y-axis)
float distance;
if (!plane.Raycast(ray, out distance)) // If the ray hits the plane
{
return _lastLookTarget;
}
Vector3 targetPoint = ray.GetPoint(distance);
RaycastHit hit;
if (Physics.Raycast(transform.position, (targetPoint - transform.position).normalized, out hit, 50f) && hit.collider.name == "AimTarget")
{
Vector3 aimTargetPosition = hit.collider.transform.position;
float distanceToTarget = Vector3.Distance(transform.position, aimTargetPosition);
float assistStrength = distanceToTarget <= minAimDistance
? 1f // Full strength if within minAimDistance
: Mathf.Clamp01(1 - (distanceToTarget - minAimDistance) / (maxAimDistance - minAimDistance));
targetPoint = Vector3.Lerp(targetPoint, aimTargetPosition, assistStrength);
//targetPoint = hit.collider.transform.position;
}
_lastLookTarget = new Vector3(targetPoint.x, transform.position.y, targetPoint.z);
return _lastLookTarget;
}
#endregion
#region Dash
void Dash()
{
if (_inputManager.sprintPerformed && !_dashing && _dashPossible)
{
StartCoroutine(DashCoroutine());
}
}
private IEnumerator DashCoroutine()
{
_dashing = true;
_dashPossible = false;
dashTrail.emitting = true;
float startTime = Time.time;
while(Time.time < startTime + dashTime)
{
_controller.Move(transform.forward * dashSpeed * Time.deltaTime);
yield return null;
}
_dashing = false;
dashTrail.emitting = false;
StartCoroutine(DashCooldown());
}
private IEnumerator DashCooldown()
{
float startTime = Time.time;
while(Time.time < startTime + dashCooldownTime)
{
yield return null;
}
_dashPossible = true;
}
#endregion
}