[SOLVED] My build system wont work and I need help with it!
This is my code for the building system and it works just fine untill I try to place a item above another one, Then it sorta freaks out and wont let me place it.
The game is a top down 2D Survival game (On the X and Y Axis).
Vector2 ClickInWorld = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 ClickInWorldHandler = new Vector2 (Mathf.Floor (ClickInWorld.x) + 0.5f, Mathf.Floor (ClickInWorld.y) + 0.5f);
RaycastHit2D hit = Physics2D.Raycast (ClickInWorldHandler, -Vector2.up, Mathf.Infinity);
// Mouse Handler
if (Input.GetMouseButtonDown (1))
{
//Building Handler
if (!hit.collider && CanBuild == true) {
GameObject NewItem = Instantiate (SelectedItem, new Vector2 (ClickInWorldHandler.x, ClickInWorldHandler.y), Quaternion.identity) as GameObject;
NewItem.name = SelectedItem.name;
NewItem.GetComponent<BuildingCore> ().Owner = gameObject;
} else if (hit.collider == true && hit.transform.gameObject.tag == "Base" && CanBuild == false) {
Debug.Log ("You have clicked: " + hit.transform.name + "! You can upgrade it or destroy it ;)");
}
}
if (CanBuild == true) {
ShadowPlacement.SetActive (true);
if (!hit.collider) {
ShadowPlacement.transform.position = ClickInWorldHandler;
ShadowPlacement.GetComponent<SpriteRenderer> ().color = Color.green;
} else {
ShadowPlacement.transform.position = ClickInWorldHandler;
ShadowPlacement.GetComponent<SpriteRenderer> ().color = Color.red;
}
} else {
ShadowPlacement.SetActive (false);
}
So if anyone could help it wont be awesome :)
Comment
Your answer
Follow this Question
Related Questions
Placing an Object at mouse position on a Terrain 3 Answers
AI Raycasting 0 Answers
Raycast from Camera 0 Answers
Raycast not consistently detecting collision. 1 Answer
How do I affect multiple objects in a single click? 0 Answers