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 /
  • Help Room /
avatar image
0
Question by zigglr · Oct 17, 2015 at 10:15 AM · c#unity5transform.position

My script to move a gameobject to the closest tagged gameobject doesn't work

I have 2 tagged gameobjects, and I would like a gameobject (which has the script below attached to it) to move to the closest gameobject that has the tag. However, it doesn't. Please can someone kindly tell me why? Thanks`enter code here`

 using UnityEngine;
 using System.Collections;
 
 public class moveBomb : MonoBehaviour
 {
 
 
     public GameObject object1;
     private GameObject[] objects1;
     private GameObject nearestTarget;
     private float distance;
     private float curDistance;
     private Vector3 pos;
     private Vector3 diff;
     private float speed = 1.5f;
 
 
     // Use this for initialization
     void Start()
     {
 
         nearestTarget = null;
         distance = Mathf.Infinity;
         pos = transform.position;
         objects1 = GameObject.FindGameObjectsWithTag("target");
         FindClosesttarget();
 
 
     }
 
     void FindClosesttarget()
     {
         foreach (GameObject obj in objects1)
         {
             diff = obj.transform.position - pos;
             curDistance = diff.sqrMagnitude;
             if(curDistance < distance)
             {
                 nearestTarget = obj;
                 object1 = nearestTarget;
                 distance = curDistance;
             }
         }
     }
 
 
 
     // Update is called once per frame
     void Update()
     {
         if (object1 != null)
         {
             transform.position = Vector3.MoveTowards (transform.position, object1.transform.position, speed);
         }
 
 
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
 
         if (col.gameObject.tag == "target")
         {
             Destroy(transform.gameObject);
 
         }
 
    
     }
 }
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

Answer by Kade Brown · Oct 18, 2015 at 03:08 AM

Try using this:

 using UnityEngine;
  using System.Collections;
  
  public class moveBomb : MonoBehaviour
  {
      private GameObject[] objects1;
      private GameObject nearestTarget;
      private float distance;
      private float curDistance;
      private Vector3 pos;
      private float speed = 1.5f;
  
  
      // Use this for initialization
      void Start()
      {
  
          nearestTarget = null;
          distance = Mathf.Infinity;
          pos = transform.position;
          objects1 = GameObject.FindGameObjectsWithTag("target");
          FindClosesttarget();
  
  
      }
  
      void FindClosesttarget()
      {
          foreach (GameObject obj in objects1)
          {
              curDistance = (obj.transform.position - pos).magnitude;
              
              if(curDistance < distance)
              {
                  nearestTarget = obj;
                  distance = curDistance;
              }
          }
      }
  
  
  
      // Update is called once per frame
      void Update()
      {
          if (nearestTarget != null)
          {
              transform.position += nearestTarget.transform.position * speed * Time.deltaTime;
          }
  
  
      }
  
      void OnTriggerEnter2D(Collider2D col)
      {
  
          if (col.gameObject.tag == "target")
          {
              Destroy(transform.gameObject);
  
          }
  
     
      }
  }
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 Kade Brown · Oct 18, 2015 at 03:17 AM 0
Share

Try to manually move it rather than using functions that move it for you because if you don't know exactly how those functions work, you may not get the result you expected. Also try to cut down on unnecessary code. For instance:

diff = obj.transform.position - pos;

curDistance = diff.sqr$$anonymous$$agnitude;

is the same as:

curDistance = (obj.transform.position - pos).magnitude;

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Movement Unit (RTS) 0 Answers

Car from Unity's standard assets giving me of null reference errors 0 Answers

How do I resolve, get_isActiveAndEnabled can only be called from the main thread error 1 Answer

How to make an object move in the direction another object is facing on 2 axis? 1 Answer

Spawn added objects into a circle 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