Hey guys.
Edit: Added images to bottom of post
I've currently managed to script my enemy shot upto a certain point, however I wish to have the enemy shoot in all directions, like a ring around it. From what I appear to understand, this will require s quick math algorithm, inputting the distance from the object, the number of projectiles and the angle for them.
So far I have this:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour
{
public float MinSpeed;
public float MaxSpeed;
public float currentSpeed;
private float x, y, z;
public GameObject Projectile;
float firingRate = 1.0f; //delay between shots, in seconds
float lastFired = -100f; //absolute time of last fired shot
// Use this for initialization
void Start()
{
//currentSpeed = Random.Range(MinSpeed, MaxSpeed);
currentSpeed = 1;
//x = Random.Range(1f, 1f);
y = 7.0f;
z = 0.0f;
transform.position = new Vector3 (x, y, z);
}
// Update is called once per frame
void Update()
{
float amtToMove = currentSpeed * Time.deltaTime;
transform.Translate(Vector3.down * amtToMove);
if (Time.time < lastFired + firingRate)
{
return;
}
if (transform.position.y <= -6.0)
{
currentSpeed = 1 ;
transform.position = new Vector3(x, y, z);
}
lastFired = Time.time;
if (transform.position.y >= 0 && transform.position.y <= 1)
{
Vector3 rightposition = new Vector3(transform.position.x + transform.localScale.x * 8, transform.position.y + transform.localScale.y * 5);
Instantiate(Projectile, rightposition, transform.rotation);
Vector3 right2position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
Instantiate(Projectile, right2position, Quaternion.Euler(0, 0, 20));
Vector3 right3position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
Instantiate(Projectile, right3position, Quaternion.Euler(0, 0, 40));
Vector3 right4position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
Instantiate(Projectile, right4position, Quaternion.Euler(0, 0, 60));
Vector3 right5position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
Instantiate(Projectile, right5position, Quaternion.Euler(0, 0, 70));
Vector3 right6position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
Instantiate(Projectile, right6position, Quaternion.Euler(0, 0, 80));
Vector3 right7position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
Instantiate(Projectile, right7position, Quaternion.Euler(0, 0, 85));
Vector3 right8position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
Instantiate(Projectile, right8position, Quaternion.Euler(0, 0, 90));
}
}
}
After searching through the forums I came upon the following topic, in which someone asked a very similar question. Shooting in Multiple Directions
He was provided with the following code.
var speed : float = 1.0;
function Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
var projectile : GameObject;
var amount : int = 16;
function Update () {
if( Input.GetButtonUp( "Jump" ) )
{
for (var i = 1; i <= amount; i++){
var instantiatedProjectile : GameObject = Instantiate( projectile, transform.position, transform.rotation );
instantiatedProjectile.transform.eulerAngles = Vector3(0, 360/amount*i, 0);
}
}
}
var projectile : GameObject;
var amount : int = 3;
function Update () {
if( Input.GetButtonUp( "Jump" ) )
{
for (var i = 0; i <= amount; i++){
var instantiatedProjectile : GameObject = Instantiate( projectile, transform.position + transform.forward*5, transform.rotation );
instantiatedProjectile.transform.eulerAngles = Vector3(0, (-30)+(60/amount*i) , 0);
}
}
}
Its clear that this script is precisely what I'm after, but unfortunately I've attempted to implement it and failed a few times. If someone could please help me to translate this script into my script it would be greatly appreciated.
