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 RichardBDev · Nov 09, 2018 at 06:18 AM · gameobjecttransformarray

How to find the transform of a GameObject from an array.

When the level starts the Main Camera should set the spawned player as its target and look at it. At game start I can't get the camera to find a Game Object with the tag "Player" and set that to Target. This isn't working though! I'm very new to c# so I could be missing something quite simple.

Any help would be greatly appreciated!

 public class LookAtTarget : MonoBehaviour
 {
     public Transform Target;
     [Range(0f,1f)]
     public float rotationSpeed = 1;
 
     void Start()
     {
          Target = GameObject.FindWithTag("Player").transform;
     }
     // Update is called once per frame
     void Update ()
     {
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * rotationSpeed);
     }
 }


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 fafase · Nov 09, 2018 at 07:45 AM 0
Share

How is the player spawned? Is it already in the scene or something is created it?

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by dan_wipf · Nov 09, 2018 at 07:05 AM

well i would avoid this method if it’s possible. i’d recomend you just to manualy asign the player to your property.

but could it be that you taged nothing as player? or wrote it wrong? to find out it it finds nothing you could debug the function.


Debug.Log(GameObject.FindWithTag(“Player”).transform);

Comment
Add comment · Show 5 · 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 RichardBDev · Nov 09, 2018 at 07:14 AM 0
Share

I was initially doing that using a public transform. However, now I have character selection implemented at the start of each level the selected character is spawned. I'm having trouble getting the spawned character and assigning it as the target for the main camera.

I've double checked the spelling of the tag and it matches the code.

avatar image Jordan4762 RichardBDev · Nov 09, 2018 at 07:55 AM 0
Share

Its possible that this code is being run before the selected character is spawned. $$anonymous$$aybe move the line to your Update: something like this?

  public class LookAtTarget : $$anonymous$$onoBehaviour
  {
      public Transform Target;
      [Range(0f,1f)]
      public float rotationSpeed = 1;
  
      void Update ()
      {
         if (Target)
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * rotationSpeed);
         }
         else
         {
             GameObject temp = GameObject.FindWithTag("Player");
             if (temp)
             {
                 Target = temp.transform;
             }
         }
      }
  }
avatar image RichardBDev Jordan4762 · Nov 09, 2018 at 11:02 AM 0
Share

I gave that a go but it still doesn't seem to work.

Show more comments
avatar image
0

Answer by digiman72 · Nov 09, 2018 at 09:04 AM

     [CODE]
 
  myTransforms = new Transform[myResTags.Count];
      for (int z = 0; z < myResTags.Count; z++)
                                 {
                                     if (myResTags[z] != null)
                                     {
                                         myTransforms.SetValue(myResTags[z].transform, z);///
                                         // Debug.Log("objects with reqTags..." + myResTags[z]);
        
                                     }
                                 }
                                 resultName = GetClosestEnemy(myTransforms).name;
 
 
     [/CODE]
 

//////////////////////////// call is, .... resultName = GetClosestEnemy(myTransforms).name;

[CODE]

 Transform GetClosestEnemy(Transform[] enemies)
     {
         Transform bestTarget = null;
         float closestDistanceSqr = Mathf.Infinity;
         Vector3 currentPosition = transform.position;
         foreach (Transform potentialTarget in enemies)
         {
             Vector3 directionToTarget = potentialTarget.position - currentPosition;
             float dSqrToTarget = directionToTarget.sqrMagnitude;
             if (dSqrToTarget < closestDistanceSqr)
             {
                 closestDistanceSqr = dSqrToTarget;
                 bestTarget = potentialTarget;
             }
         }
         return bestTarget;
     }

[/CODE]

a little more complicated for what you might need, but very usefull for finding closest target, the thing is to store whatever you want in the array into a list first, then set the values of element in the transform array, i used physics.overlap sphere to find objects of same tag in the radius, then i add them to a list, i set my transform array length or count to the list count, and then just set the values of each element, once you got your array, then you just call ex: target = GetClosestEnemy(myTransforms).name

Comment
Add comment · Show 1 · 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 fafase · Nov 09, 2018 at 09:48 AM 0
Share

That is not what he is looking for.

avatar image
0

Answer by Miso21 · Nov 09, 2018 at 04:57 PM

try with LookAt function.

  public class LookAtTarget : MonoBehaviour
 {
  public Transform Target;
  [Range(0f,1f)]
  public float rotationSpeed = 1;
 
  void Start()
  {
       transform.LookAt(Target);
  }
  // Update is called once per frame
  void Update ()
  {
      transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * rotationSpeed);
  }

}

Comment
Add comment · Show 2 · 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 RichardBDev · Nov 09, 2018 at 09:26 PM 0
Share

I will try the LookAt function.

The public transform Target is never being assigned to the spawned rocket. I tried using the method Awake ins$$anonymous$$d of Start on the character spawner script however, that still didn't work.

Again I've double checked my na$$anonymous$$g conventions and there aren't any errors there.

Gah! What's going on?

avatar image Miso21 RichardBDev · Nov 10, 2018 at 09:07 AM 0
Share

try to assign camera

      public class LookAtTarget : $$anonymous$$onoBehaviour
      {
        public Transform camera;
        public Transform Target;
        [Range(0f,1f)]
       public float rotationSpeed = 1;
  
   void Start()
   {
        camera.transform.LookAt(Target);
   }
   // Update is called once per frame
   void Update ()
   {
       transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * rotationSpeed);
   }

avatar image
0

Answer by RichardBDev · Nov 11, 2018 at 11:03 PM

So!

It turns out my initial script worked fine. Just a bug in unity. Once I had restarted few times it seemed to work just fine!

Thank you for everyone who helped.

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

169 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 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

How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 Answers

How to convert GameObject array to Transform array in C#? 2 Answers

Remove an object from an array and destroy it (C#) 2 Answers

Instantiate Game Object on Array of Transforms 1 Answer

Find all GameObjects with tag and add them to a transform array 2 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