I’m currently working on a SHMUP/bullet hell game. I’ve started coding the player to shoot a laser. The problem is that every time I test the game, the laser doesn’t shoot right. It shoots in the correct direction (just up) if the ship stays still or move up and down. The moment it moves either left or right, the laser curves to whichever side it moved to. Here’s my code:
public class playerFire : MonoBehaviour {
public GameObject laserPrefab;
public float fireDelay = 0.05f;
float cooldownTimer = 0;
void Update(){
cooldownTimer -= Time.deltaTime;
if(Input.GetButton("Fire1") && cooldownTimer <= 0){
cooldownTimer = fireDelay;
Instantiate (laserPrefab, transform.position, transform.rotation);
}
}
}
I’d appreciate if someone could help me fix my code.
The problem doesn’t have anything to do with the fire delay or cooldown. This just helps with fire rate.
Well, the laser projectile is instantiated so it is rotated the same way as the ship, so if the ship if tilted to the left, so is the projectile. If the laser should always shoot straight up, you would have to change it’s rotation. If it’s a 2D game where x is the left-right axis and y the up-down axis, you would use:
Note that transform.forward, transform.rotation etc. is always relative to the local rotation, while Vector3.up, Vector3.right etc. is in global/world space (where forward is always along the positive axis, right is always the positive x axis, etc.).