Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by xyHeat · May 21, 2016 at 03:39 PM · systembuildingrts

RTS Building : Buildings following the mouse [C#]

Hi everybody ! I'm making an RTS game and i have a problem for the building system : my buildings move only on the x axis...

This is my actual code :

 using UnityEngine;
 using System.Collections;
 
 public class Building : MonoBehaviour{
 
     public GameObject[] structuresTypes;
     Vector3 mousePos;
     GameObject _structureTypes;
     GameObject pre_structure;
     bool isBuilding = false;
 
 
     void Update()
     {
         float x = Input.mousePosition.x;
         float z = Input.mousePosition.z;
         float y = 0.25f;
         mousePos = new Vector3(x, y, z);
 
         if(isBuilding == true)
         {
             pre_structure.transform.position = mousePos;
             if (Input.GetMouseButtonDown(0))
             {
                 Destroy(pre_structure);
                 Instantiate(_structureTypes, mousePos, Quaternion.identity);
                 isBuilding = false;
             }
         }
     }
 
 
     public void SelectFarm()
     {
         _structureTypes = structuresTypes[0];
         isBuilding = true;
         Destroy(pre_structure);
         pre_structure = (GameObject)Instantiate(_structureTypes, mousePos, Quaternion.identity);
     }
 
 
 }


Thank you, bye xyHeat

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ninja_gear · May 21, 2016 at 04:13 PM

The mouse uses a Vector2 to position. I know it takes and returns a Vector3 for its position, but really the Z is unused. When you get your mouse position you are getting the position of it on the ScreenSpaceUIOverlay (the 'Canvas' object that gets drawn over the scene). The mouse will never return its position in the world space. There are functions for finding a world position relative to the screen and a screen position (like the Mouse) relative to the world. Remember, when going from Screen space to World space (or back), you need a camera. What you want to do is something LIKE:

          Vector3 calculatePosition;
     
          void Update()
          {
              calculatePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     
              if(isBuilding == true)
              {
                  pre_structure.transform.position = calculatePosition ;
                  if (Input.GetMouseButtonDown(0))
                  {
                      Destroy(pre_structure);
                      Instantiate(_structureTypes, mousePos, Quaternion.identity);
                      isBuilding = false;
                  }
              }
          }
 

What this code will do is draw a line from the camera, at that position on the screen, and return the point of the first object it hits in the game world.

ProTip: You probably don't want to have every building in the scene calculating the Mouse's position every frame. You can solve this problem by creating a Mouse 'Singleton', and having that singleton calculate its position, then use that stored information in the rest of your code.

Comment
Add comment · Show 4 · 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
avatar image xyHeat · May 21, 2016 at 05:28 PM 0
Share

It doesn't work, the GameObject doesn't move. I'll try to use an empty game object to store the position of the mouse and then use it for the placement

avatar image ninja_gear xyHeat · May 22, 2016 at 03:26 AM 0
Share

Well, I probably should have asked how you implemented your map. If you are using screen space for the map (your game is 2D) then you use $$anonymous$$ouse X and Y (not x and z like your above script). If your map is NOT on the screen (if your game is 3D) then you need Camera.main.ScreenToWorldPoint().

avatar image xyHeat ninja_gear · May 22, 2016 at 07:21 AM 0
Share

It's a 3D game, when i use Camera.main.ScreenToWorldPoint(Input.ousePosition), It don't move, and it is the same if i use an empty gameObject for the position.

Show more comments

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

42 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a electric/energy system? 1 Answer

[SCRIPTERS] Character "fly" system like gmod or minecraft. 1 Answer

Strategy GUI for building information 0 Answers

Terrian blocks dissappearing after baking Navmesh 1 Answer

Rts game. 2d unit on 3d scene 0 Answers


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