- Home /
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);
}
}
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.
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
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 :)
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
Follow this Question
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