twinStick/twinStickCrawler/Assets/Scripts/Creatures/Player/PlayerAimRotate.cs
2024-12-24 14:34:15 +01:00

49 lines
1.7 KiB
C#

using System.Collections.Generic;
using UnityEngine;
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);
}
}