using System.Collections; using System.Collections.Generic; using UnityEngine; public class LockYToGround : MonoBehaviour { public LayerMask groundMask; public float offsetY = 0f; void LateUpdate() { Ray ray = new Ray(transform.position + Vector3.up, Vector3.down); if (Physics.Raycast(ray, out RaycastHit hit, 10f, groundMask)) { Vector3 pos = transform.position; pos.y = hit.point.y + offsetY; transform.position = pos; } } }