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);
}
}
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*);*
}
}
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.