I have a spaceship which fires shots straight ahead. This works so far.
But I want some shots that move diagonally, too. Also I’d like to know how can I rotate the bullets in the appropriate direction. Can someone help, please?
This is the bullet-script:
public float damage = 25f;
float speed;
void Start () {
speed = 8f;
}
void Update () {
//get the bullet's current position
Vector2 position = transform.position;
//compute the bullet's new position
position = new Vector2(position.x, position.y + speed * Time.deltaTime);
//update the bullet's position
transform.position = position;
//this is the top right point of the screen
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));
//if the bullet went outside the screen on the top, then destroy the bullet
if (transform.position.y > max.y) {
// this is just an object-pool
PlayerControl.bulletPool.ReturnInstance(gameObject);
}
}
This is the playershoot-script:
public GameObject bulletPosition01;
public GameObject playerShotsGO;
void Update () {
//fire bullets when the spacebar is pressed
if (Input.GetKeyDown ("space")) {
Shoot();
}
//function to make the player shoot
public void Shoot () {
//play the laser sound effect
SoundManager.PlaySFX (shootAudio);
//get bullet from object-pool
GameObject bullet01 = bulletPool.GetInstance (playerShotsGO);
bullet01.transform.position = bulletPosition01.transform.position;
bullet01.transform.SetParent (playerShotsGO.transform, true);
}