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 /
  • Help Room /
avatar image
0
Question by Vanderley5974 · Sep 20, 2020 at 02:53 AM · transformtransform.lookat

Transform.Lookat only looks at list, not the nearest tagged

Hi! I'm really new to scripting and all, but i'm trying to get my object to look at the nearest object with a specific tag! however with this code i've written it's only looking at the nearest object from a list, say for example, its looking at "SoulShard", then "SoulShard (1)", "SoulShard (2)", etc. instead of the nearest soulshard with the tag, could yall help me out?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LookAt : MonoBehaviour
 {
     private Transform target;
 
     public Transform playerObject;
 
     private float distance;
     private float minimumDistance = 5f;
 
     void Start()
     {
 
     }
 
     private void Update()
     {
         target = GameObject.FindWithTag("MinimapFinder").transform;
         distance = Vector3.Distance(target.transform.position, transform.position);
 
         if (distance <= minimumDistance)
         {
             playerObject.transform.LookAt(target);
         }
         Vector3 direction = target.position - transform.position;
     }
 }
 
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 streeetwalker · Sep 20, 2020 at 05:57 AM

@Vanderley5974, stick with it - it takes some time to get fluent enough that you will feel that you can solve any problem - but they still take work. (I don't know about being "good at it").

It seems like you want to get all gameObjects with a specifc tag and find the nearest one?

If so, then you have 2 separate problems: 1.) How to get a list of all objects with a specific tag. 2.) how to find the one in the list nearest to you.

GameObject.FindWithTag will only get you 1 random gameObject that is tagged with the Tag you specify. There is no way using that function to get all gameObjects with a Tag.

Furthermore, any of the Find operations are expensive in terms of processing and you're better off avoiding them where ever possible. If you have a small game with few objects it isn't an issue. In a large game with a lot of objects it can be a major issue.

To solve problem #1 you need another strategy. It is nearly always a good idea to keep references to all objects in your game using an array or list by building the list upfront, and if you are dynamically adding objects add a reference to each new object when you instantiate them. If you've place the objects into the game manually declare the array/list as a public variable and add them to the script's inspector field.

To solve problem #2, iterate over the list use any of the Find Smallest Value algorithms, such as this example:

 //assume all of the gameObjects in the is list are tagged with your specific tag
 // example list declaration
 List<GameObject> happyGameObjects = new List<GameObject>();
 //assuming the list is populated:
 float minDistance = Mathf.Infinity;
 GameObject foundGameObject = null;
 for( int i= 0; i < happyGameObjects.Count; i++ ) {
        GameObject currentGO = happlyGameObjects[ i ];
        // The next two lines are less expensive than the Distance function
        // assumes this script is on the object we are measuring from
        //     these two lines can be combined
        float direction = currentGO.transform.postition - transform.position;
        float distance = direction.sqrMagnitude;
        // record the shortest distance
        if( distance < minDistnace ){
                minDistance = distance;
                foundGameObject = currentGO;
        }
 }
 // that's it - the nearest gameObject is referenced in foundGameObject when the loop is done



(Note that I am not using foreach to iterate over the list because it too is expensive, especially if your doing mobile games, so as a matter of practice I avoid it where ever possible.)

There are certainly many ways to do both steps 1 and 2.

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 Vanderley5974 · Sep 20, 2020 at 07:24 AM 0
Share

i'll see what i can do, many thanks! =) I'm pretty tired right now and it's late and I just wanna get it working, so i'll figure something out

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

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

tranform.LookAt(playerTarget) ; 2D Beat em up enemy script 0 Answers

transform.lookat not working? 0 Answers

what is the difference between transform.look at and quaternion.look rotation 0 Answers

Why does my transform.lookat not work? 1 Answer

Rigidbody2D figits when pushed against an object. 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