Created
October 9, 2017 00:28
-
-
Save unity3dcollege/03931762ae7337986f9bf2d7f830036a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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