So my design is there is a bullet gun and bullet itself, the gun set the direction and the bullet script normalize the direction vector for transformation. Now I want to make a spiral patterm, my idea is allow gun scipt to add angle each time the fireenemybullet is called but some how the direction vector seems not updating and bullet fly in a stright line.
This is the ememy gun where the direction is set and updated
public class stg8emgun3 : MonoBehaviour
{
public GameObject YukariBullet3;
float maxSpawnRateInSeconds = 0.5f;
// Use this for initialization
void Start ()
{
InvokeRepeating("FireEnemyBullet", 0.5f, 0.1f);
InvokeRepeating("IncreaseSpawnRate", 0.5f, 0.1f);
}
// Update is called once per frame
void Update ()
{
}
public void FireEnemyBullet()
{
//Get a reference to player
GameObject player01 = GameObject.Find("player00 animation_0");
if (player01 != null)//if the player is still alive
{
//instantiate enemy bullet
GameObject bullet0 = (GameObject)Instantiate(YukariBullet3);
Vector2 direction = new Vector2(0,1);
bullet0.GetComponent<YukariBullet3>().SetDirection(direction);
direction.x += 0.1f; //This is where the problem is,seems this doesn add to the X in vector
BulletSpawnTimer();
}
}
void BulletSpawnTimer()
{
float spawnInsecond;
if (maxSpawnRateInSeconds > 0.2f)
{
spawnInsecond = Random.Range(0.3f, maxSpawnRateInSeconds);
}
else
{
spawnInsecond = 0.2f;
}
Invoke("BulletSpawnTimer", spawnInsecond);
}
// Funcrtion to increase the dificulty of the game
void IncreaseSpawnRate()
{
if (maxSpawnRateInSeconds > 0.5f)
{
maxSpawnRateInSeconds -= 0.08f;
}
if (maxSpawnRateInSeconds <= 0.2f)
{
//CancelInvoke("IncreaseSpawnRate");
//CancelInvoke("SpawnEnemy");
CancelInvoke("FireEnemyBullet");
CancelInvoke("IncreaseSpawnRate");
}
}
}
And this is the bullet script where the direction is recived
public class YukariBullet3 : MonoBehaviour {
float speed;//Bullet Speed
Vector2 _direction; //Bullet direction
bool isReady;//to know when it is ready
void Awake()
{
speed = 2f;
isReady = false;
}
// Use this for initialization
void Start ()
{
}
//set bullet direction
public void SetDirection(Vector2 direction)
{
// set direction normalize, to unit vector
_direction = direction.normalized;
isReady = true;
}
// Update is called once per frame
void Update ()
{
if (isReady)
{
//get bullet pos.
Vector2 position = transform.position;
//Compute current pos.
position += _direction * speed * Time.deltaTime;
//pos. update
transform.position = position;
//Remove bullet when it fly outside
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));
//detect if it is outside
if ((transform.position.x < min.x) || (transform.position.y < min.y)
|| (transform.position.x > max.x) || (transform.position.y > max.y))
{
Destroy(gameObject);
}
}
}
}