- Home /
Help With LookAt Target
Hello,
I'm trying to get this to work. What I want is for the enemy to find any tags of "Enemy" and then for the script to LookAt it whilst changing its speed. But I keep getting an error like cannot convert to Transform, or GameObject.
//var target : Transform; var speed : float = 5;
function Update () {
var enemy = GameObject.FindWithTag("Enemy");
transform.LookAt(enemy);
enemy.transform.Translate(Vector3.forward*speed*Time.deltaTime);
}
Answer by SteveFSP · Nov 28, 2010 at 06:21 PM
The problem appears to be the LookAt statement. The documentation indicates its argument is a Transform, but "enemy" is a GameObject.
So...
function Update () {
var enemy = GameObject.FindWithTag("Enemy");
transform.LookAt(enemy.transform); // <<<< Change here.
enemy.transform.Translate(Vector3.forward*speed*Time.deltaTime);
}
Some possible enahancements...
function Update () {
var enemy = GameObject.FindWithTag("Enemy");
if (enemy != null)
{
var enemyLocation = enemy.transform;
transform.LookAt(enemyLocation);
enemyLocation.Translate(Vector3.forward*speed*Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
How do you Find Object through tags in javascript? 3 Answers
How can I make a Transform that I dont have to set each time I place a prefab? 1 Answer
Can't Return the Tag of a Child Object 4 Answers
modifing enemies target inside a radius 1 Answer
Change a Transform to Aim to the center of a Collider. Not (0,0,0) 2 Answers