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 /
avatar image
0
Question by Topthink · Nov 23, 2016 at 07:39 PM · movementgameobjectcolliderobjectforeach

How do I get a single object to move?

I have multiple spheres and I want to move them independently. I want to "left-click-mouse" to select the sphere I want to move. Then I want to "right-click-mouse" to select a location for the sphere to move to. In theory, I should (I want to...) be able to move ALL the spheres simultaneously but each to a different location. I'm not getting any errors...but I'm not getting any movement either. Any suggestions?

 void Update()
 {

 /////  I select the object here...

     if (Input.GetMouseButtonDown(0))  
     {
         RaycastHit hit; 
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast(ray, out hit)) 
         {

             Collider col = hit.collider;
             if (col != null) 
             {
                 test01 = hit.collider.name;
                 Debug.Log (hit.collider.name);
             }
         }
     }

 /////  I select the movement destination here...

     if (Input.GetMouseButtonDown(1))  
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast(ray, out hit)) 
         {
             goToHere = hit.point;
             Debug.Log (hit.point);
         }
     }

 /////  I (attempt) to get the object moving here by using foreach to check through every object and move the one with correct name

     foreach (GameObject gameObj in GameObject.FindObjectsOfType<GameObject>())
     {
         if (gameObj.name == test01)
         {
             transform.position = Vector3.MoveTowards (transform.position, goToHere, speed * Time.deltaTime);
         }
     }


 }
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
Best Answer

Answer by christoph_r · Nov 23, 2016 at 08:59 PM

  • Calling FindObjectsOfType is very slow and should be avoided in Update().

  • You are actually moving the GameObject with the movement script. You need to specify the GameObject or Transform that you want to move, otherwise transform is a reference to the transform of the GameObject your script is attached to.

  • Instead of referencing by name, why not reference the game object directly? Simply create a variable of type GameObject or Transform. You can then assign it through: myGameObject = hit.collider.gameObject; in line 16 in your snippet.

Comment
Add comment · Show 3 · 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 Topthink · Nov 23, 2016 at 10:11 PM 0
Share

Thank you for your 3 comments.

Paragraph one: $$anonymous$$y RTS won't have much in it in terms of objects so I don't know if being "slow" will really hurt things. Thank you though.

Paragraph two: I don't really understand what you are saying here. I really appreciate your help...I just don't fully understand what you are saying. I apologize.

Paragraph three: $$anonymous$$y impression is this will be very helpful. I will try it immediately. Thanks.

Again, thank you for taking the time to help me.

avatar image christoph_r Topthink · Nov 23, 2016 at 10:34 PM 0
Share

There's hardly ever a reason to constantly search all objects, it's usually a good idea to simply store references. Even if it works fine in your current game, it's just one of those things you shouldn't make a habit of. Eventually you'll end up calling it on multiple objects and your scene grows and the performance overhead becomes more and more noticeable etc.

Regarding Game Object references: Every GameObject in your scene has a Transform attached to it. The GameObject class keeps a reference to its transform in a field handily called transform. In order to move a specific object, you must of course call the methods and change the fields of that object's transforms. To do that, you need a reference to that transform, or in other words a member variable of the type transform (or game object, and then use myGameObject.transform). Changing fields of just transform will move and rotate the GameObject your script is attached to, as it is basically the same reference as gameObject.transform.

avatar image Topthink christoph_r · Nov 23, 2016 at 11:50 PM 0
Share

Your explanations are excellent and I can tell it took you some effort to share all that. I'll exa$$anonymous$$e carefully everything you mentioned and I'm sure it will help me produce a better program.

I appreciate the time you gave me to help me understand Unity a little better.

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

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

Related Questions

Character Controller Follow me in the air 1 Answer

How can I move an object physically correct around an other object? 1 Answer

Moving objects using raycasting? 1 Answer

Ignore Collider 0 Answers

checking colliders from multiple raycast 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