Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Created October 9, 2017 00:28
Show Gist options
  • Save unity3dcollege/03931762ae7337986f9bf2d7f830036a to your computer and use it in GitHub Desktop.
Save unity3dcollege/03931762ae7337986f9bf2d7f830036a 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