AMJV_UNITY/TP3/Assets/Scripts/CarThings/RotateWithMouse.cs
2024-12-10 22:32:20 +01:00

34 lines
936 B
C#

using UnityEngine;
public class RotateWithMouse : MonoBehaviour
{
[SerializeField] LayerMask layerToIgnore;
[SerializeField] Transform targetTransform;
[Range(0f, 1f)]
public float lerpSpeed = 0.01f;
Rigidbody rb;
Camera cam;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody>();
cam = Camera.main;
}
// Update is called once per frame
void FixedUpdate()
{
Ray _ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(_ray, out RaycastHit hitInfo, float.MaxValue, ~layerToIgnore))
{
Vector3 point = hitInfo.point;
Vector3 dir = (point - transform.position).normalized;
dir.y = 0f;
targetTransform.forward = Vector3.Lerp(targetTransform.forward, dir, lerpSpeed);
}
}
}