• 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 Sskethan · Mar 16, 2016 at 07:29 PM · targetingfindwithtag

Target closet object with tag and get transform?

What I'm trying to do is detect if the object with a certain tag is within range and if it is rotate the gun to face the targeted object. What I have now works on 1 object by name, but can't figure out how to detect the object if it has another name. I have tried to use findobjectswithtag but am having no luck.

 using UnityEngine;
 using System.Collections;
 
 public class LaserLook : MonoBehaviour {
 
     public Transform target;
     public float objectDistance;
     public float rotationDamping;
 
     void Start () {
 
         target = GameObject.Find ("Random Material").GetComponent<Transform> ();
 
     }
 
     void Update () {
 
         objectDistance = Vector3.Distance (target.position, transform.position);
 
         if (objectDistance < 20f) 
         {
             lookAtPlayer();
         }    
     }
 
     void lookAtPlayer()
     {
         Quaternion rotation = Quaternion.LookRotation (target.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
     }
 }
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 Positive7 · Mar 16, 2016 at 08:45 PM

  using UnityEngine;
     using System.Collections;
     
     public class Test : MonoBehaviour
     {
     
         public GameObject[] target;
         public float objectDistance;
         public float rotationDamping;
     
         void Start()
         {
     
             target = GameObject.FindGameObjectsWithTag("Enemy");     //It will look for all objects tagged as "Enemy"
     
         }
     
         void Update()
         {
             for (var i = 0; i < target.Length; i++)
             {
                 objectDistance = Vector3.Distance(target[i].transform.position, transform.position);
     
                 if (objectDistance < 20f)
                 {
                     lookAtPlayer();
                 }
             }
         }
     
         void lookAtPlayer()
         {
             for (var i = 0; i < target.Length; i++)
             {
                 Quaternion rotation = Quaternion.LookRotation(target[i].transform.position - transform.position);
                 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
             }
         }
     }
 
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 Sskethan · Mar 17, 2016 at 12:14 AM 0
Share

Thank you, this works perfectly.

avatar image Sskethan · Mar 17, 2016 at 12:46 PM 0
Share

Ok, I ran into a problem with this code. The code works great if there is only 1 object with tag, but if there are multiple objects the axis no longer points at the object I'm flying toward. Here is a video capture of whats happening. The Random $$anonymous$$aterial on the left and the Random Object on the right both have the same tag. When I fly toward one the axis points away from the object ins$$anonymous$$d of toward. Not sure what the problem here is. Please help. Thank you. link text

avatar image Positive7 Sskethan · Mar 17, 2016 at 03:01 PM 0
Share

Hmm... the code should look for the closest object so it should work, but I'll double check it

avatar image Positive7 Sskethan · Mar 17, 2016 at 03:44 PM 0
Share

Try this :

 using UnityEngine;
 
 public class LaserLook : $$anonymous$$onoBehaviour
 {
     public float rotationDamping;
     public float objectDistance;
     public GameObject[] target;
 
     GameObject FindClosestAsteroid()
     {
         target = GameObject.FindGameObjectsWithTag("Asteroid");
         GameObject closestTarget = null;
         var distance = $$anonymous$$athf.Infinity;
         var position = transform.position;
         foreach (GameObject go in target)
         {
             Vector3 distanceDiff = go.transform.position - position;
             float curDistance = distanceDiff.sqr$$anonymous$$agnitude;
             if (curDistance < distance)
             {
                 closestTarget = go;
                 distance = curDistance;
             }
         }
         return closestTarget;
     }
 
     void Update()
     {
         var distanceFromTarget = Vector3.Distance(transform.position, FindClosestAsteroid().transform.position);
         if (objectDistance > distanceFromTarget)
         {
             RotateLaser();
         }
         else {
             RotateLaserDefault();   //Reset Laser position to zero
         }
     }
 
     void RotateLaser()
     {
             Quaternion rotation = Quaternion.LookRotation(FindClosestAsteroid().transform.position - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
     }
     void RotateLaserDefault() {
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, Time.deltaTime * rotationDamping);
     }
 }
avatar image Sskethan · Mar 17, 2016 at 11:44 PM 0
Share

Yes. Thank you sooo much. Now it is working the way it should.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

50 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

Related Questions

How do I work with two ENUMS 2 Answers

Camera change targets when new object appears or Camera change targets by pressing a button 1 Answer

Character continues moving when locked onto an enemy 1 Answer

Will delete objects in Editor but not in Build? 0 Answers

How can I check what the closest object to a player is that he's rotating towards without using a raycast? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges