Shooting in 45 degrees – 2d shooter game

Updated on October 30, 2017 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on October 26, 2017

Hi guys

I’m stuck on something that maybe is a really easy thing to do, but my knowledge is limited

I want that my character shoots in 45 degrees angles when I press the direction key + up key

This is a new feature that I wanted to implement to my test game, so by now I have this code:

using UnityEngine;
using System.Collections;
public class BasicWeapon : MonoBehaviour {
 public GameObject projectile;
 public string shootKey;
 public Vector2 speed;
 bool canShoot= true;
 public Vector2 offset = new Vector2(4f,0.1f);
 public float cooldown = 1f;
 public GameObject playerDir;
 //trying to flip sprite
// Use this for initialization
 void Start () {
 // get a reference to the SpriteRenderer component on this gameObject
// GameObject g = GameObject.Find("Player Two");
 // Update is called once per frame
 void Update () {
 if (Input.GetKeyDown (shootKey) && canShoot){
 if (playerDir.transform.localScale.x < 0 && projectile.transform.localScale.x > 0) {
 Debug.Log ("left");
 projectile.transform.localScale *= -1;
 } if (playerDir.transform.localScale.x > 0 && projectile.transform.localScale.x < 0) {
 Debug.Log ("right");
 projectile.transform.localScale *= -1;
 GameObject go = (GameObject) Instantiate (projectile, (Vector2)transform.position + offset * transform.localScale.x, Quaternion.identity);
 go.GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed.x * transform.localScale.x, speed.y);
 StartCoroutine (CanShoot ());
 IEnumerator CanShoot()
 canShoot = false;
 yield return new WaitForSeconds (cooldown);
 canShoot = true;


But this depends on the direction of the player only. What should I use to get a different angle for the proyectile?

I was trying to use Quaternion.AngleAxis with no sucess.


Any help will be appreciated.

Sorry if it is a dumb question, I’m really new to this.



  • Liked by
2 on October 27, 2017

Firstly, when a key for a certain direction is pressed, you want to first be facing that direction, adjusting your world co-ordinates accordingly.


Then, when the up key is pressed to fire, you need to create an offset to compensate for the 45 degrees you want to be firing at, assumingly you don’t want to have the same offset for every direction you face (Meaning that it’ll fire in the same position at 45 degrees), in this case, you’ll need a ran(random) to ensure that after a direction is picked, it fires RANDOMLY on a 45 degree curve.


Since i don’t do unity programming, i can only give you a logical stand-point and offer you a summary of how to achieve what you want too, i wish you luck! 🙂 

on October 30, 2017

Thanks for your response, I understand your logic, but idk how to do it in unity really.

on October 30, 2017


It’s great to hear back from you.

I have limited time as of right now and so sadly I will not have the time to go and find out for you, but I can guide you best I can meanwhile:

You want to be working with and find out how to use keyboard press events, use the random function, those are the main things.

Have a look into those and see how you do, I will try and help as much as I can today when the time is available and see what I can find.

Good luck!

Show more replies
  • Liked by