Skip to content

Instantly share code, notes, and snippets.

@andrew-raphael-lukasik
Last active April 11, 2024 22:52
Show Gist options
  • Select an option

  • Save andrew-raphael-lukasik/49db89424f11d3edd937c536e5b41d54 to your computer and use it in GitHub Desktop.

Select an option

Save andrew-raphael-lukasik/49db89424f11d3edd937c536e5b41d54 to your computer and use it in GitHub Desktop.
object orientation placement schemes
// src* = https://gist.github.com/andrew-raphael-lukasik/49db89424f11d3edd937c536e5b41d54
using UnityEngine;
public class LetsRotateAndSpawn : MonoBehaviour
{
[SerializeField] GameObject _prefab = null, _prefabPreview = null;
[SerializeField] Vector3 _planeOrigin = Vector3.zero , _planeNormal = Vector3.up;
GameObject _instancePreview = null;
Vector3 _cursorPos = Vector3.zero, _cursorDragPosStart = Vector3.zero;
Quaternion _cursorRot = Quaternion.identity, _cursorDragRotStart = Quaternion.identity;
Camera _camera = null;
void Start ()
{
_camera = Camera.main;
_instancePreview = GameObject.Instantiate( _prefabPreview );
_instancePreview.SetActive( false );
}
void Update ()
{
Plane plane = new Plane( inNormal:_planeNormal , inPoint:_planeOrigin );
var ray = _camera.ScreenPointToRay( Input.mousePosition );
if( plane.Raycast( ray , out var hitDist ) )
{
Vector3 hitPoint = ray.origin + ray.direction * hitDist;
_cursorPos = hitPoint;
// store rot pivot info
if( Input.GetMouseButtonDown(1) )
{
_cursorDragPosStart = hitPoint;
_cursorDragRotStart = _cursorRot;
}
// update rotation
if( Input.GetMouseButton(1) )
{
Vector3 vec = _cursorPos - _cursorDragPosStart;
float screenPxDist = Vector2.Distance( _camera.WorldToScreenPoint(_cursorDragPosStart) , _camera.WorldToScreenPoint(_cursorPos) );
if( vec.sqrMagnitude>0 )
_cursorRot = Quaternion.Slerp(
_cursorDragRotStart ,
Quaternion.LookRotation( vec , _planeNormal ) ,
Mathf.SmoothStep( 0 , 1 , screenPxDist / ( Mathf.Min(Screen.width,Screen.height) * 0.05f ) )
);
}
// update preview mesh
if( !_instancePreview.activeSelf ) _instancePreview.SetActive( true );
_instancePreview.transform.SetPositionAndRotation( hitPoint , _cursorRot );
// spawn prefab
if( Input.GetMouseButtonDown(0) )
GameObject.Instantiate( _prefab , hitPoint , _cursorRot );
}
else _instancePreview.SetActive( false );
}
void OnDrawGizmos ()
{
if( _camera!=null && Input.GetMouseButton(1) )
{
Gizmos.color = Color.yellow;
Vector3 offset = new Vector3{ z=_camera.nearClipPlane*2f };
Gizmos.DrawLine(
_camera.ScreenToWorldPoint( Vector3.Scale( _camera.WorldToScreenPoint(_cursorDragPosStart) , new Vector3{x=1,y=1} ) + offset ) ,
_camera.ScreenToWorldPoint( Vector3.Scale( _camera.WorldToScreenPoint(_cursorPos) , new Vector3{x=1,y=1} ) + offset )
);
}
Gizmos.matrix = Matrix4x4.TRS( _planeOrigin , Quaternion.AngleAxis(0,_planeNormal) , Vector3.one );
Gizmos.DrawWireCube( Vector3.zero , new Vector3{ x=100 , z=100 } );
}
}
// src* = https://gist.github.com/andrew-raphael-lukasik/49db89424f11d3edd937c536e5b41d54
using UnityEngine;
public class LetsSpawnAndRotate : MonoBehaviour
{
[SerializeField] GameObject _prefab = null, _prefabPreview = null;
GameObject _instance = null;
Vector3 _pos, _normal;
Vector2 _click;
void Update ()
{
var camera = Camera.main;
var ray = camera.ScreenPointToRay( Input.mousePosition );
if( Input.GetMouseButtonDown(0) )
{
if( Physics.Raycast(ray,out var hit) )
{
_pos = hit.point;
_normal = hit.normal;
_instance = GameObject.Instantiate(
_prefabPreview ,
_pos ,
Quaternion.AngleAxis( 90 , Quaternion.LookRotation(_normal)*Vector3.right ) * Quaternion.LookRotation(_normal)
);
_click = Input.mousePosition;
}
}
if( _instance!=null )
{
Vector2 mouseDelta = (Vector2) Input.mousePosition - _click;
if( Physics.Raycast(ray,out var hit) && (mouseDelta).sqrMagnitude>0 )
{
float yaw = -Mathf.Atan2( mouseDelta.y , mouseDelta.x );
Quaternion rotBase = Quaternion.AngleAxis( 90 , Quaternion.LookRotation(_normal)*Vector3.right ) * Quaternion.LookRotation(_normal);
Quaternion rotAux = Quaternion.AngleAxis( Mathf.Rad2Deg*yaw , _normal );
_instance.transform.rotation = rotAux * rotBase;
}
if( Input.GetMouseButtonUp(0) )
{
GameObject.Instantiate( _prefab , _instance.transform.position , _instance.transform.rotation );
Destroy( _instance );
_instance = null;
}
}
}
void OnDrawGizmos ()
{
Gizmos.DrawRay( _pos , _normal );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment