51 lines
1.8 KiB
C#
51 lines
1.8 KiB
C#
|
using UnityEngine;
|
||
|
|
||
|
namespace Creatures.Player.Movement
|
||
|
{
|
||
|
public class PlayerAimRotate : MonoBehaviour
|
||
|
{
|
||
|
Canvas _canvas;
|
||
|
|
||
|
public void initialize(Canvas canvas)
|
||
|
{
|
||
|
_canvas = canvas;
|
||
|
}
|
||
|
|
||
|
Vector3 _lastLookTarget;
|
||
|
public Vector3 AimTarget(float minAimDistance, float maxAimDistance, Camera camera, Vector3 mouseInput)
|
||
|
{
|
||
|
Ray ray = camera.ScreenPointToRay(mouseInput); // 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, maxAimDistance, LayerMask.GetMask("Enemy")))
|
||
|
{
|
||
|
Vector3 aimTargetPosition = hit.collider.transform.position;
|
||
|
float distanceToTarget = Vector3.Distance(transform.position, aimTargetPosition);
|
||
|
|
||
|
float assistStrength = Mathf.Clamp(1-((maxAimDistance - minAimDistance) / (distanceToTarget - minAimDistance)), 0f, .6f);
|
||
|
|
||
|
targetPoint = Vector3.Lerp(targetPoint, aimTargetPosition, assistStrength);
|
||
|
//targetPoint = hit.collider.transform.position;
|
||
|
}
|
||
|
_lastLookTarget = new Vector3(targetPoint.x, transform.position.y, targetPoint.z);
|
||
|
|
||
|
return _lastLookTarget;
|
||
|
}
|
||
|
|
||
|
public void ApplyRotation(Vector3 aimTarget)
|
||
|
{
|
||
|
aimTarget.y = _canvas.transform.position.y;
|
||
|
_canvas.transform.LookAt(aimTarget);
|
||
|
}
|
||
|
}
|
||
|
}
|