Skip to content

Instantly share code, notes, and snippets.

@bfollington
Created June 23, 2019 00:40
Show Gist options
  • Save bfollington/b1cd795cd6c016690854638eb9190495 to your computer and use it in GitHub Desktop.
Save bfollington/b1cd795cd6c016690854638eb9190495 to your computer and use it in GitHub Desktop.
Isometric mouse cursor snapping
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100, FloorLayer)) {
Transform objectHit = hit.transform;
if (SnapToGrid) {
var newPos = new Vector3(Round(hit.point.x, GridSize), _yOffset, Round(hit.point.z, GridSize));
if (_targetPosition != newPos) {
Tween.Position(transform, newPos, TweenSpeed, _yOffset, Tween.EaseOut);
_targetPosition = newPos;
}
} else {
transform.position = new Vector3(hit.point.x, _yOffset, hit.point.z);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment