Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Major · Dec 23, 2012 at 06:27 PM · turretplacementmoneytower

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Dec 27, 2012 at 01:06 PM 0
Share

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 =]

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges