- Home /
Placing tiles around the mouse position
I have this code to add a tile to the world when the mouse is clicked in the editor screen, my problem is I would like to make it place in a circle radius around the mouse so I can put tiles in large areas. Here is the section of code that places a tile at the moment:
if(e.isMouse && e.type == EventType.MouseDown && e.button == 0 ){
GUIUtility.hotControl = controlID;
e.Use();
GameObject gameObject;
Transform prefab = grid.tilePrefab;
if(prefab){
Undo.IncrementCurrentGroup();
gameObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab.gameObject);
Vector3 allign = new Vector3(Mathf.Floor(mousePos.x / grid.width) * grid.width + grid.width / 2f,
Mathf.Floor(mousePos.y / grid.height) * grid.height + grid.height / 2f, 0f);
if(GetTransformFromPosition(allign) != null){
DestroyImmediate(gameObject);
return;
}
gameObject.transform.position = allign;
gameObject.transform.parent = GameObject.Find("Level").transform;
Undo.RegisterCreatedObjectUndo(gameObject, "Create " + gameObject.name);
}
}
Anyone got a clue on how to do this ? Thanks.
EDIT: Each tile is 8 units wide in case that matters to anybody
Just a side note, I think na$$anonymous$$g a GameObject gameObject is not a good idea. Like caling a Transform transform... I wonder how that don't triggers some heavy warnings at least :p
Plus it makes the understanding of your code harder.
Sorry if it causes any confusion, that's just the way I have been doing it for years now so it makes sense to me :)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Initialising List array for use in a custom Editor 1 Answer
Multiple Cars not working 1 Answer
Editor Script renderer.bounds.intersect does not work 0 Answers