I know this probably a simple question but I have a problem with one of my enemies in my game. I’ve created a script that I set on the prefab of this enemy, but the problem is that I spawn out four enemies of the same sort, but I want these to fly in there own direction with the help of an assigned array that set the value that they should fly in.
But how can I set the value and make the first ball called “explodingball1” go in the 45 angle direction, then for explodingball2 I want to set the direction 135, explodingball 3 225 direction etc…
Note that I currently use a Random.range to set the direction of the 4 balls, but as I said they fly off in on of these four random directions that I set.
Here’s the code:
using UnityEngine;
using System.Collections;
public class EnemyExplodeBallScript : MonoBehaviour
{
Rigidbody2D bouncing;
public float speed = 30;
public float timer = 3.0f;
Vector3 velocity;
float[] angleArray;
public GameObject explodingBall1;
public GameObject explodingBall2;
public GameObject explodingBall3;
public GameObject explodingBall4;
void Start()
{
if(GameObject.Find("Enemy_explode(Clone)").tag == "EnemyExplodeMiddle")
{
//Create 4 angles for the ball to
angleArray = new float[4];
//Array values
angleArray[0] = 45;
angleArray[1] = 135;
angleArray[2] = 225;
angleArray[3] = 315;
transform.rotation = Quaternion.identity;
// Random range where the ball is supposed to fly within the array
int selection = Random.Range(0, angleArray.Length);
float angle = angleArray[selection];
for(int i = 0; i < angleArray.Length; i++)
{
**//Should I use a for loop?**
}
// Calculate velocity for ball
velocity = new Vector2 (Mathf.Cos (angle*Mathf.Deg2Rad), Mathf.Sin(angle*Mathf.Deg2Rad));;
// Get the ball moving!
this.GetComponent<Rigidbody2D> ().velocity = velocity * speed;
}
}