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 Abdulraahman · Jul 08, 2017 at 07:59 AM · gameobjecttagfollowarrow

Arrow follows nearest target please help

Hi,

How to make the arrow follow object with "Enemy" tag

Here is the script :

 #pragma strict
 var Speed : float = 20;
 var relativeDirection = Vector3.forward;
 var duration : float = 1.0;
 var shooterTag : String = "Player";
 var hitEffect : GameObject;
 
 function Start () {
     hitEffect = GetComponent(BulletStatus).hitEffect;
     GetComponent(Rigidbody).isKinematic = true;
     Destroy();
 }
 
 
 function Update () {
     
     var absoluteDirection : Vector3 = transform.rotation * relativeDirection;
     transform.position += absoluteDirection *Speed* Time.deltaTime;
 
 
 }
 
 function Destroy(){
     Destroy (gameObject, duration);
 
 }
 
 
 function OnTriggerEnter (other : Collider) {
 
   if (other.gameObject.tag == "Wall") {
         if(hitEffect){
             Instantiate(hitEffect, transform.position , transform.rotation);
         }
         Destroy (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
Best Answer

Answer by FlaSh-G · Jul 08, 2017 at 12:00 PM

 import System.Collections.Generic;

 private var targets : List.<Transform> = new List.<Transform>();
 public var speed : float = 1;
 
 function Awake()
 {
   var targetGOs = GameObject.FindGameObjectsWithTag("Enemy");
   for(var i = 0; i < targetGOs.length; i++)
   {
     targets.Add(targetGOs[i]);
   }
 }
 
 function Update()
 {
   var direction = Vector3.zero;
   var distance = Mathf.Ininity;

   for(var i = 0; i < targets.Count; i++)
   {
     var thisDirection = target.position - transform.position;
     var thisDistance = thisDirection.sqrMagnitude;
     if(thisDistance < distance)
     {
       direction = thisDirection;
       distance = thisDistance;
     }
   }

   if(direction != Vector3.zero)
   {
     transform.forward = direction; // Look at the target
     transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime); // Move towards target
   }
 }

Technically, FixedUpdate would be more viable than Update, but that would cause a bunch of new problems that need some additional in-depth knowledge to solve.

Edit: Edited the code to look for the closest enemy. This code is just a starting point. For example, it misses removing destroyed enemies from the list.

Comment
Add comment · Show 4 · 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 Abdulraahman · Jul 08, 2017 at 01:13 PM 0
Share

Thank you for replying, Now it follows objects with "Enemy" tag but there are two problems 1 - it follows random objects not the nearest 2 - it hit the ground under the object

avatar image FlaSh-G · Jul 08, 2017 at 01:30 PM 0
Share

Edited my answer.

In terms of going through the floor - the $$anonymous$$oveTowards line completely ignores all possible collisions with anything. You'd have to replace it with something else, but I can't tell you what because that depends on how your game works. You could, for example, use a Rigidbody, a CharacterController or a Nav$$anonymous$$eshAgent. Which one you use highly depends on what you're trying to accomplish, so it's entirely beyond this question :)

avatar image Abdulraahman · Jul 08, 2017 at 01:46 PM 0
Share

I have got an error in "import"

avatar image FlaSh-G · Jul 08, 2017 at 02:08 PM 0
Share

If you ever have an error you want help with, always post what the error is. The chance of someone being able to help you with the information "an error" is very slim.

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

92 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

Related Questions

GameObject follow Tagged Object 1 Answer

Find all gameObjects with same tag 1 Answer

Destroying Multiple Objects with Tags 1 Answer

switching between 2 states 1 Answer

Follow Player when Close C# Help 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