Files
beyond/Assets/Scripts/Tools/LookAtInEditMode.cs
2024-11-20 15:21:28 +01:00

80 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Beyond
{
[ExecuteInEditMode]
public class LookAtInEditMode : MonoBehaviour
{
#if UNITY_EDITOR
public GameObject lookAtMe;
public bool updateY;
public bool forward = true;
public bool grounded;
public LayerMask ground = ~0;//1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 + 512 + 1024 + 2048 + 4096 + 8192 + 16384;
public Vector3 groundOffset;
public Vector3 rotationOffset;
[Space]
public bool stopRotating;
public bool resetToOrginal;
private Vector3 targetPoint;
private Quaternion targetRotation;
private Vector3 startPosition;
private Quaternion startRotation;
private void Start()
{
startPosition = transform.position;
startRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
if (resetToOrginal)
{
resetToOrginal = false;
transform.position = startPosition;
transform.rotation = startRotation;
}
if (lookAtMe != null && !stopRotating)
{
if (updateY)
{
targetPoint = new Vector3(lookAtMe.transform.position.x, lookAtMe.transform.position.y, lookAtMe.transform.position.z) - transform.position;
}
else
{
targetPoint = new Vector3(lookAtMe.transform.position.x, transform.position.y, lookAtMe.transform.position.z) - transform.position;
}
if (forward)
{
targetRotation = Quaternion.LookRotation(targetPoint, Vector3.up);
}
else
{
targetRotation = Quaternion.LookRotation(-targetPoint, Vector3.up);
}
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 1f) * Quaternion.Euler(rotationOffset);
}
if (grounded)
{
grounded = false;
Ray ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, out RaycastHit hit, float.MaxValue, ground))
{
transform.position = hit.point + groundOffset;
}
}
}
private void OnDrawGizmos()
{
Gizmos.DrawRay(transform.position, transform.forward);
}
#endif
}
}