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>();
|
|
|
|
}
|
2024-12-18 14:19:25 +00:00
|
|
|
|
2024-12-17 21:46:05 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-18 14:19:25 +00:00
|
|
|
Vector3 _lastMoveDir = Vector3.zero;
|
2024-12-17 21:46:05 +00:00
|
|
|
Vector3 MoveDir(Vector3 input)
|
|
|
|
{
|
|
|
|
if (_dashing)
|
|
|
|
{
|
|
|
|
return Vector3.zero;
|
|
|
|
}
|
|
|
|
|
|
|
|
Quaternion camRotation = Quaternion.Euler(0f, _cameraGameObject.transform.rotation.eulerAngles.y, 0f);
|
|
|
|
input = camRotation * input;
|
2024-12-18 14:19:25 +00:00
|
|
|
|
|
|
|
_lastMoveDir = input;
|
2024-12-17 21:46:05 +00:00
|
|
|
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);
|
|
|
|
|
2024-12-18 14:19:25 +00:00
|
|
|
float assistStrength = Mathf.Clamp(1-(distanceToTarget - minAimDistance) / (maxAimDistance - minAimDistance), 0f, .6f);
|
2024-12-17 21:46:05 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2024-12-18 14:19:25 +00:00
|
|
|
_controller.Move(_lastMoveDir * dashSpeed * Time.deltaTime);
|
2024-12-17 21:46:05 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|