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
-1
Question by putlucky · Mar 22, 2015 at 05:54 PM · c#gameobjectpositionupdate

C# Trouble getting object's current position on mouse down.

I have a script which allows the user to instantiate a house object and have that object stick to the mouse position - but so far it can't place the house anywhere, permanently.

I originally tried to get the current transform of my house object within Update but then realised that because of it's nature, the method would be continually well... updating the transform. So I removed that logic and placed it into another method with some success. Pressing the lmb: I can see the object 'blip', which suggests that it's attempting to stay there but the Update function is somehow getting in the way.

Sorry for the comments, they help me think:

 public GameObject house; 
     public GameObject instantiatedHouse; 
     Vector3 placement = new Vector3();
     Vector3 currentPos = new Vector3(); 
 
     // Use this for initialization
     void Start () {
         //Load the object into the game.
         house = Resources.Load("Medieva_fantasy_house", typeof(GameObject)) as GameObject;
     }
 
 
     
     // Update is called once per frame
 
     void Update () {
         //Cast a ray once per frame from the mouse position to the terrain. 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         
         if (Physics.Raycast (ray, out hit)) 
         {
             //Determines point of instantiation. 
             placement = hit.point; 
         }
 
         //Check once per frame if the house has been instantiated.
         //This is a costly way of doing it, not a permenant solution. 
         if (GameObject.FindGameObjectsWithTag ("MyHouse") != null) {
             //Instantiated so set house to raycast's point of contact. 
 
             instantiatedHouse.transform.position = placement;
 
             if(Input.GetMouseButtonDown(0)){
 
 
                 instantiatedHouse.transform.position = currentPos; 
                      
 
             }
 
         } else {
             Debug.Log("House not instantiated.");
         }
     }
 
     public void InstantiateObject()
     {
         //This method is called by the button in an OnClick() event. 
 
         //Instantiate the object at mouse position along terrain. 
         
     instantiatedHouse = Instantiate (house, placement, Quaternion.Euler(0, 0, 0)) as GameObject; 
     
 
     }
 
     public Vector3 CurrentPosition()
     {
             //Get the current position of the house as a Vector3. 
             currentPos = instantiatedHouse.transform.position;
 
             //Set the position of the house to equal the position stored in currentPos. 
         //Hopefully this position will not continually update. 
             instantiatedHouse.transform.position = currentPos; 
         
         /*return the Vector3 which dictates where the object should be placed. currentPos is only invoked when 
          * user presses lmb down. 
         */
         return currentPos; 
 
         //THIS DOESN'T WORK. 
     }
 }

Like I said, I'm seeing a 'blip' on mouse down and then it continues sticking to my mouse position. This is of course not what I want, so thanks to anyone who tries to help me figure out what's going wrong.

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Update character's position after animation 1 Answer

Resize gameObject when it has extended a restricted area 0 Answers

Trigger LateUpdate on inactive gameObjects 2 Answers

Check if player object is right above this object 1 Answer

How can I get exact screen position of 3D game object in front of camera while using cardboard in VR game? 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