- Home /
Placing objects in TD Game
I am creating a TD and I have come to the point where I need to be able to place down objects based on where I click with the mouse. I don't want there to be a grid system because I think it's to constricting for the user. I don't understand this area of unity, so any help would be very much appreciated! Here is the script I am using:
static var Kills : int = 0;
static var Money : int = 250;
static var Lives : int = 100;
static var curExp : int;
var Paused : boolean = false;
var maxExp : int = 50;
var Turret_Rifel : Texture;
var Turret_Mini : Texture;
var Turret_Rocket : Texture;
var level : int;
var mySkin : GUISkin;
var ShowMenu : boolean = false;
function Update()
{
if(Input.GetKeyDown(KeyCode.P)&& !Paused)
{
Paused = true;
Time.timeScale = 0;
}
else if(Input.GetKeyDown(KeyCode.P)&& Paused)
{
Paused = false;
Time.timeScale = 1;
}
if(Input.GetKeyDown(KeyCode.Alpha1))
{
Time.timeScale = 1;
}
if(Input.GetKeyDown(KeyCode.Alpha2))
{
Time.timeScale = 2;
}
if(Input.GetKeyDown(KeyCode.Alpha3))
{
Time.timeScale = 3;
}
if(Lives <= 0)
{
Lives = 0;
Loose();
}
if(curExp >= maxExp)
{
curExp = 0;
maxExp = maxExp * 1.5;
level++;
}
if(Money <= 0)
{
Money = 0;
}
}
function OnGUI()
{
GUI.skin = mySkin;
GUI.Box(Rect(Screen.width / 2 - 60, 10, 60, 20), "" + Kills);
GUI.Box(Rect(Screen.width / 2, 10, 60, 20), Lives + "%");
GUI.Box(Rect(10, 10 , 60, 20), "$" + Money);
if(GUI.Button(Rect(70, 10, 25, 20), "X"))
{
if(ShowMenu == false)
ShowMenu = true;
else
ShowMenu = false;
}
if(ShowMenu == true)
{
if(GUI.Button(Rect(10, 40, 60, 60), Turret_Rifel))
{
if(Money < 350)
{
}
else
Money -= 350;
}
if(GUI.Button(Rect(10, 100, 60, 60), Turret_Mini))
{
if(Money < 2000)
{
}
else
Money -= 2000;
}
if(GUI.Button(Rect(10, 160, 60, 60), Turret_Rocket))
{
if(Money < 1000)
{
}
else
Money -= 1000;
}
}
if(Paused)
{
if(GUI.Button(Rect(Screen.width / 2 - 30, Screen.height / 2 - 10, 60, 20), "Exit"))
{
Application.LoadLevel(0);
}
if(GUI.Button(Rect(Screen.width / 2 - 30, Screen.height / 2 + 20, 60, 20), "Quit"))
{
Application.Quit();
}
}
GUI.Box(Rect(50, Screen.height - 25, (curExp * 200)/maxExp, 20), curExp + "/" + maxExp);
}
function Loose()
{
Application.LoadLevel(0);
Time.timeScale = 1;
Kills = 0;
Money = 250;
Lives = 10;
}
The area where I want to put the placement code is after the if(showMenu == true) and then in those area where It is subtracting money.
not easy. Think of a RTS game, before you place a building, there is a Footprint of the building that follows the mouse. If there is something in the way, the footprint turns red and the building cannot be placed.
Create an object that is positioned based on a raycast. Have that object check for trigger collisions. Then use a list to remember what objects are in the way, e.g. On Enter, something is in the way therefore not buildable. On Exit, check the list for that object, remove that object from the list. Then, simply check the list count. If there are no items in the list, then there is nothing in the way of the footprint, therefore the area is clear and buildable =]
Answer by MaGuSware™ · Dec 27, 2012 at 01:35 PM
Something similar has been asked before: http://answers.unity3d.com/questions/242023/raycast-an-object-to-the-mouse-position.html
I hope this is enough information to get you going.
Ray vRay = myCam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(vRay, out hit))
{
hit.point; // <-- this is your position.
}
Your answer
Follow this Question
Related Questions
Turret AI doesnt hit enemy 2 Answers
Animate an object 1 Answer
How to make the turret not placeable on walls? 1 Answer
tower defense turret turns right by 90degree 0 Answers
Problem with Orthographic Camera 1 Answer