1
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – DS94 Mar 29 '16 at 7:49
0
\$\begingroup\$

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));
    }

}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.