# Instantiate a 2D GameObject in certain direction

I'm working in a Top-Down 2D Game, and i'm trying to avoid the use of rigidbodies and rotations, i manage the direction the player is facing via input and changing the sprites, but right now, i dont know how to shoot a bullet in the direction i'm facing.

I check the direction with the current SpriteRenderer.sprite, but i dont know how to Instantiate a GameObject like

Instantiate(bullet, transform.position, /** One of the Four Angles like (0,90,180,270)*/);


Any idea on how to aproach this?

Thank you.

You just need to create a Quaternion with the appropriate rotation. Since you're top-down, you can create a rotation around the up axis (or the forward axis, depending on how you have things laid out).

Quaternion.AngleAxis(90, Vector3.up)


So,

Instantiate(bullet, transform.position, Quaternion.AngleAxis(180, Vector3.up));


For other info about creating 2D games, check out the Unity tutorials for 2D Game Creation.

• Thank you for your answer, in my case, i had to use Vector3.forward instead of Vector3.up since my rotation had to be applied on the Z axis (2D depht) – DS94 Mar 29 '16 at 7:49

For future reference, i'm posting how i got this to work.

//I just change the current sprite through Input
//And move the bullet with
//tranform.Translate(Vector2.right * speed) inside their own script

void ShootingManager(){

if(spriteRenderer.sprite == sprite_left){
Instantiate (bullet, this.transform.position, Quaternion.AngleAxis (180, Vector3.forward));
}
else if(spriteRenderer.sprite == sprite_right){
Instantiate (bullet, this.transform.position, Quaternion.AngleAxis (0, Vector3.forward));
}
else if(spriteRenderer.sprite == sprite_up){
Instantiate (bullet, this.transform.position, Quaternion.AngleAxis (270, Vector3.forward));
}
else if(spriteRenderer.sprite == sprite_down){
Instantiate (bullet, this.transform.position, Quaternion.AngleAxis (90, Vector3.forward));
}

}