I have a simple question but somehow can’t find a way to solve it. I want this blue cube parallel to the grey slope. But I made the slope via Probuilder so it is just a model with no rotation. So I can’t set an angle to rotate the cube ( Also manually calculating the angle doesn’t work)
- Add this script to your project
- Move this object over the slope
- Press
Ctrl + g
or clickGameObject/Align To Ground
AlignToGroundMenuCommand.cs
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
namespace EditorOnly
{
public static class AlignToGroundMenuCommand
{
[MenuItem( "CONTEXT/GameObject/Align To Ground" )]
public static void AlignToGround_ctx ( MenuCommand command )
{
GameObject gameObject = (GameObject) command.context;
AlignToGround( gameObject );
}
[MenuItem( "GameObject/Align To Ground %g" )]
public static void AlignToGround_cmd ()
{
foreach( var gameObject in Selection.gameObjects )
AlignToGround( gameObject );
}
static void AlignToGround ( GameObject gameObject )
{
Undo.RegisterFullObjectHierarchyUndo( gameObject , nameof(AlignToGround) );
Transform transform = gameObject.transform;
var hits = Physics.RaycastAll( transform.position , Vector3.down , maxDistance:10f );
foreach ( var hit in hits )
if( hit.transform!=transform )
{
transform.position = hit.point;
transform.rotation = Quaternion.LookRotation( Vector3.ProjectOnPlane(transform.forward,hit.normal).normalized , hit.normal );
break;
}
}
}
}
#endif