Skip to content

Instantly share code, notes, and snippets.

@CodeSmile-0000011110110111
Last active March 5, 2023 10:49
Show Gist options
  • Save CodeSmile-0000011110110111/86734d625169a4708407d95bc200e05e to your computer and use it in GitHub Desktop.
Save CodeSmile-0000011110110111/86734d625169a4708407d95bc200e05e to your computer and use it in GitHub Desktop.
Script that drops an object to ground (collider) and optionally rotates to that surface
// Public Domain
using UnityEngine;
namespace CodeSmile
{
[ExecuteInEditMode]
public class DropToGround : MonoBehaviour
{
[SerializeField] private bool m_SnapPosition;
[SerializeField] private Vector3 m_PositionOffset;
[SerializeField] private bool m_SnapPositionToBounds;
[SerializeField] private bool m_SnapRotation;
[SerializeField] private bool m_SnapCollider;
private Vector3 m_EditorPosition;
private Quaternion m_EditorRotation;
private void Update()
{
var offset = 1000f;
var rayOrigin = transform.position + Vector3.up * offset;
var hits = Physics.RaycastAll(rayOrigin, Vector3.down, offset * 2f, Physics.AllLayers);
foreach (var hit in hits)
{
if (hit.transform == transform)
continue;
var posOffset = Vector3.zero;
if (m_SnapPositionToBounds)
{
var collider = GetComponent<Collider>();
if (collider != null)
posOffset.y = collider.bounds.size.y / 2f;
}
if (m_SnapPosition)
transform.position = hit.point + posOffset + m_PositionOffset;
if (m_SnapRotation)
{
var rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
var angles = rotation.eulerAngles;
angles.y = m_EditorRotation.y;
transform.rotation = Quaternion.Euler(angles);
}
break;
}
}
private void OnEnable()
{
m_EditorPosition = transform.position;
m_EditorRotation = transform.rotation;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment