Hello, I'm new to this forum so please be patient. I have a problem where I instantiate a projectile once the player gets close to the enemy. However the same code works when the player shoots. Here's a portion of the player shooting code:
//If left mouse button is pressed, fire.
if (Input.GetMouseButtonDown (0)) {
Fire();
}
}
//If player hits a deadly object, the level is restarted
void OnCollisionEnter2D(Collision2D col){
if(col.gameObject.tag == "Deadly"){
Application.LoadLevel(0);
}
}
//Create new instance of the bullet, and if facing right, shoot to the right, and if left, shoot to the left.
void Fire(){
GameObject clone = Instantiate (projectile, projSpawn.position, projSpawn.rotation)as GameObject;
if(!facingLeft){
clone.rigidbody2D.velocity = new Vector2(60,0);
clone.rigidbody2D.AddForce(new Vector2(100,0));
}
if(facingLeft){
clone.rigidbody2D.velocity = new Vector2(-60,0);
clone.rigidbody2D.AddForce(new Vector2(-100,0));
}
}
and here is a portion of the enemy shooting code:
void Attack(int Direction){
//Attack mode is set to true
attackMode = true;
transform.localScale = new Vector3(Direction, 1, 1);
if(Direction == -1)
facingLeft = true;
else
facingLeft = false;
Fire ();
}
//Create new instance of the bullet, and if facing right, shoot to the right, and if left, shoot to the left.
void Fire(){
//Creates a instance of the bullet
GameObject clone = Instantiate (projectile, projSpawn.position, projSpawn.rotation)as GameObject;
//If facing right the bullet is sent along the positive x axis
if(!facingLeft){
clone.rigidbody2D.velocity = new Vector2(60,0);
clone.rigidbody2D.AddForce(new Vector2(100,0));
}
//If facing left, the bullet is sent along the negative x axis
if(facingLeft){
clone.rigidbody2D.velocity = new Vector2(-60,0);
clone.rigidbody2D.AddForce(new Vector2(-100,0));
}
}
As you can see the code is the same for both but is called in different conditions, this is where my problem lies as my player can shoot fine but the enemy's bullet won't fire correctly. Please help if you can, I've been stuck with this for days.
↧