Skip to content

Instantly share code, notes, and snippets.

@ircnelson
Forked from unity3dcollege/CubePlacer.cs
Created May 25, 2018 12:50
Show Gist options
  • Save ircnelson/ed2092b5ec94ff78c6001c5e0e8d05a2 to your computer and use it in GitHub Desktop.
Save ircnelson/ed2092b5ec94ff78c6001c5e0e8d05a2 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class CubePlacer : MonoBehaviour
{
private Grid grid;
private void Awake()
{
grid = FindObjectOfType<Grid>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo))
{
PlaceCubeNear(hitInfo.point);
}
}
}
private void PlaceCubeNear(Vector3 clickPoint)
{
var finalPosition = grid.GetNearestPointOnGrid(clickPoint);
GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = finalPosition;
//GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = nearPoint;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment