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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Stealthygolem · Oct 15, 2014 at 07:44 AM · c#gameobjecttransformlist

Calling GameObject's transform from list

Hello! I am trying to call a target from a list. I am trying to reference the transform.position for the gameObject I am calling (closestPlayer) so I can "MoveTowards" it. How do I do this? I've been looking at this yesterday and today getting addingly smarter (and frustrated), but I can't seem to get it to work. Everything else works fine, though! :) Relevant code:

 public class EnemyAI : MonoBehaviour {
 
     private GameObject closestPlayer;
 
     public float speed;
     public float maxDistance;
 
     public GameObject FindClosestPlayer() {
         GameObject[] gos;
 
         gos = GameObject.FindGameObjectsWithTag("Player");
 
         maxDistance = 300f;
         Vector3 position = transform.position;
         foreach (GameObject currentPlayer in gos) {
             Vector3 diff = currentPlayer.transform.position - position;
             float curDistance = diff.sqrMagnitude;
 
             if (curDistance < maxDistance) {
                 closestPlayer = currentPlayer;
                 maxDistance = curDistance;
             }
         }
         return closestPlayer;
     }
 
     void Update () {
         GameObject closestPlayer = FindClosestPlayer();
 //        transform.position = Vector2.MoveTowards(the closestPlayer <--

As you can see for the last line of code, I want to reference the "closestPlayer" correctly, so my EnemyAI can move towards it! I appreciate any help, thank you.

Comment
Add comment · Show 8
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 Fornoreason1000 · Oct 15, 2014 at 08:15 AM 1
Share

Wisearn, the OP is right to use sqr$$anonymous$$agnitude... Sqr$$anonymous$$agnitude is used becuase it dodges a lengthy Square root function that computers just hate it( magnitude is equal to the square root of the sum of each axis difference)... in short its a performance thing

you want to sort out the closest player right? basically sort by distance calculations...

http://answers.unity3d.com/questions/341065/sort-a-list-of-gameobjects-by-distance.html http://answers.unity3d.com/questions/246781/sort-transforms-by-distance-to-player.html

give these a read... if you still having trouble let me know

you last line of code would be

 transform.position = Vector2.$$anonymous$$oveToward(transform.position, closestPlayer.transform.position,maxDistance) 


http://docs.unity3d.com/ScriptReference/Vector2.$$anonymous$$oveTowards.html (this ones a little vague, so here a link to Vector2.Lerp as well) http://docs.unity3d.com/ScriptReference/Vector2.Lerp.html

avatar image Stealthygolem · Oct 15, 2014 at 08:17 AM 0
Share

I played around with some code, this seemed to work fine. What do you guys think, any flaws?

         closestPlayer.transform.Translate (0, 0, 0);
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, closestPlayer.transform.position, speed * Time.deltaTime);
avatar image Kiwasi Stealthygolem · Oct 15, 2014 at 08:32 AM 0
Share

No comments as answers please

The first line of your code in this comment is useless and can be deleted. The second line looks good.

avatar image Wisearn · Oct 15, 2014 at 08:36 AM 0
Share

OP is right to use sqr$$anonymous$$agnitude if you are comparing one sqr$$anonymous$$agnitude to another sqr$$anonymous$$agnitude, but sqr$$anonymous$$agnitude will not represent an actual distance (300f "maxDistance" would be something like 17 units and not 300 units in your scene).

In the code from the unity scripting reference they use $$anonymous$$athf.Infinity as a "maxDistance" which makes sense because it's not a real number (unlike 300).

But yes sorry you don't need to use $$anonymous$$ath.Abs since magnitude already does this.

avatar image Stealthygolem · Oct 15, 2014 at 08:38 AM 0
Share

I'm sorry, I thought to myself that it could be a potential answer to my own question (which it sort of was), that is why I didn't use it as a comment. But, I will consider not doing so in the future, thanks for the notice! Thank you for the comment, btw. Cheers

avatar image Kiwasi · Oct 15, 2014 at 08:53 AM 0
Share

All good. I just reread your answer. I was probably a little harsh on my moderation. Sorry about that.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Kiwasi · Oct 15, 2014 at 08:10 AM

You could simply use

 closestPlayer.transform.postion;

Its better to work with transforms directly if you are mostly interested in position. Change the return type of FindClosestPlayer to a transform. Then you could use

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

31 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

Related Questions

What is the best way to instatiate an array of connected GameObjects? 0 Answers

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers

Subtracting the position of transform from the position of Game Objects in a list. 1 Answer

How to select an Game Object from an Item List 0 Answers

How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 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