-
-
Save IanPedroV/b7952998da7e06fb86ea0831af1bc863 to your computer and use it in GitHub Desktop.
This file contains 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
public class GridHighlight : MonoBehaviour | |
{ | |
public Transform tilePrefab; | |
public Transform newTile; | |
public Vector3 objectPos; | |
Vector3 mousePos; | |
private Grid grid; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
private void Awake() | |
{ | |
grid = FindObjectOfType<Grid>(); | |
} | |
void OnMouseOver() | |
{ | |
if (!(Input.mousePosition.x == mousePos.x && Input.mousePosition.y == mousePos.y)) | |
{ | |
if (newTile != null) | |
{ | |
Destroy(newTile.gameObject); | |
} | |
mousePos = Input.mousePosition; | |
mousePos.z = 70f; | |
Vector3 objectPos = Camera.current.ScreenToWorldPoint(mousePos); | |
objectPos.y = 1.05f; | |
// newTile = Instantiate(tilePrefab, objectPos, Quaternion.Euler(0,0,0)) as Transform; | |
RaycastHit hitInfo; | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
if (Physics.Raycast(ray, out hitInfo)) | |
{ | |
PlaceTileNear(hitInfo.point); | |
} | |
} | |
} | |
private void PlaceTileNear(Vector3 clickPoint) | |
{ | |
var finalPosition = grid.GetNearestPointOnGrid(clickPoint); | |
newTile = Instantiate(tilePrefab, finalPosition, Quaternion.Euler(0, 0, 0)) as Transform; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment