twinStick/twinStickCrawler/Assets/Scripts/Creatures/Player/PlayerRotate.cs
2024-12-20 15:21:20 +01:00

35 lines
1.4 KiB
C#

using UnityEngine;
public class PlayerRotate : MonoBehaviour
{
Vector3 _lastLookTarget;
public Vector3 LookTarget(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, 50f) && hit.collider.name == "AimTarget")
{
Vector3 aimTargetPosition = hit.collider.transform.position;
float distanceToTarget = Vector3.Distance(transform.position, aimTargetPosition);
float assistStrength = Mathf.Clamp((distanceToTarget - minAimDistance) / (maxAimDistance - 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;
}
}