Hi,
I have this game where enemy spaceships are spawned randomly in the world and then should follow and shoot the player.But the problem is that when I use the LookAt(target) function and then assign my player object the assignment is not made in the prefab.
So, when running the game, the enemy is instantiated from the prefab the var target is missing.
So, how do I fix this, or is there any other method apart from LookAt(target) to make things work?
Create_Alien.js —>
var aliens : Rigidbody[]; ///Array to hold different types of ships
private var sec : int; ///Time Delay
private var savedTime : int; ///Stuff
var vel : float = 10; ///Velocity of the spawned object
var h_range : float = 10; ///The max-min horizontal spawn range
var ePos : float = 50; ///Distace from origin where spawn takes place
var density : float = 10; ///No of objects spawned at a time
function Start () {
}
function Update () {
sec = Time.time*density;//shoot at regular intervals//
create(sec);
}
////This function Instantiates the object at a random point on a plane////
function create(sec){
if(sec!=savedTime){
var clone : Rigidbody;
clone = Instantiate(aliens[Random.Range(0,aliens.Length)],
Vector3(Random.Range(-h_range,h_range),Random.Range(0,0.4),ePos),
Random.rotation);
// Give the cloned object an initial velocity
clone.velocity = transform.TransformDirection (Vector3.back * vel);
savedTime = sec;
}
}
Attack.js —>
function Start () {
}
var target : Transform; //The Player
var moveSpeed : float = 10; //Movement speed
function Update () {
/////Begin looking at target(Player) and move towards it./////
transform.LookAt(target);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
/////When the ship is close enough to the player/////
function OnTrriggerEnter(hit : Collider)
{
///Stop!///
if(hit.gameObject.tag == "Player")
{
moveSpeed = 0;
///Begin shooting///
///////////////////
}
}
ThankYou.