The script I am creating allows an enemy in the game to move towards the object with the tag “Player.” When applied to my enemy in a scene the enemy will not move towards the player. Here is the attempted code:
#pragma strict
var player: GameObject;
var target : Transform;
var rb: Rigidbody;
function Start () {
player = GameObject.FindWithTag("Player");
}
function Update () {
target = player.transform;
transform.LookAt(target);//always looks at target every update
rb.AddForce(transform.forward * 20);//accelerates towards target
}
Additionally, I also want this code to work when the object is instantiated. As the player walks into a event trigger the enemy spawns. I want the enemy to move towards the player when created as well.
Thank you in advanced for you help!