Make projectile fire diagonally

Hi guys, im really new to unity and c#. I’ve got a script with a player that I want to fire a projectile based on the direction its facing. It seems to work fine going left, right, up and down, just not diagonally. I see why it won’t work because the variables can cancel each other out when 2 are pressed, but after an hour of searching I can’t seem to find a way to fix it:

	void Update () {
		
		if (Input.GetKey (KeyCode.UpArrow)) {
			transform.position += (new Vector3 (0f, speed, 0f) * Time.deltaTime);
			projectileYSpeed = 5f;
			projectileXSpeed = 0f;
		} 
		
		if (Input.GetKey (KeyCode.RightArrow)) {
			transform.position += (new Vector3 (speed, 0f, 0f) * Time.deltaTime);
			projectileXSpeed = 5f;
			projectileYSpeed = 0f;
		} 

		if (Input.GetKey (KeyCode.DownArrow)) {
			transform.position += (new Vector3 (0f, -speed, 0f) * Time.deltaTime);
			projectileYSpeed = -5f;
			projectileXSpeed = 0f;
		}
		
		if (Input.GetKey (KeyCode.LeftArrow)) {
			transform.position += (new Vector3 (-speed, 0f, 0f) * Time.deltaTime);
			projectileXSpeed = -5f;
			projectileYSpeed = 0f;
		}

			if (Input.GetKeyDown (KeyCode.Space)) {
				InvokeRepeating ("Fire", 0.00001f, firingRate);
			}
			if (Input.GetKeyUp (KeyCode.Space)) {
				CancelInvoke ("Fire");
			}
	}
	
	void Fire(){
		Vector3 offset = new Vector3 (0f, 0.3f, 0f);
		GameObject beam = Instantiate (bullet, transform.position + offset, Quaternion.identity) as GameObject;
		beam.GetComponent<Rigidbody2D> ().velocity = new Vector3 (projectileXSpeed, projectileYSpeed, 0);
	}

You are almost there, buddy! You already recognized that if two keys are pressed, the second variable setting will override the first setting.

So think of the two axis (vertical and horizontal) as independent. Each pair only sets its corresponding direction. If you press up, you only set the Y to 5 and leave the X alone (as upwards only influence vertical direction). But of course, then don’t forget to set the speed to 0 if neither up nor down has been pressed, but left or right has.

     if (Input.GetKey (KeyCode.UpArrow)) {
         transform.position += (new Vector3 (0f, speed, 0f) * Time.deltaTime);
         projectileYSpeed = 5f;
     } 
     
     if (Input.GetKey (KeyCode.RightArrow)) {
         transform.position += (new Vector3 (speed, 0f, 0f) * Time.deltaTime);
         projectileXSpeed = 5f;
     } 

     if (Input.GetKey (KeyCode.DownArrow)) {
         transform.position += (new Vector3 (0f, -speed, 0f) * Time.deltaTime);
         projectileYSpeed = -5f;
     }
     
     if (Input.GetKey (KeyCode.LeftArrow)) {
         transform.position += (new Vector3 (-speed, 0f, 0f) * Time.deltaTime);
         projectileXSpeed = -5f;
     }
     
     if (!Input.GetKey (KeyCode.UpArrow) && !Input.GetKey (KeyCode.DownArrow) && (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.LeftArrow))) {
         projectileYSpeed = 0;
     }

     if (!Input.GetKey (KeyCode.RightArrow) && !Input.GetKey (KeyCode.LeftArrow) && (Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.DownArrow))) {
         projectileXSpeed = 0;
     }

Mh… woah, that looks like a mess. Lets clean this code up and make it a lot more readable. Then you probably got the idea much better! :wink:

bool up = Input.GetKey (KeyCode.UpArrow);
bool down = Input.GetKey (KeyCode.DownArrow);
bool left = Input.GetKey (KeyCode.LeftArrow);
bool right = Input.GetKey (KeyCode.RightArrow);
bool vertical = up || down;
bool horizontal = left || right;
bool any = vertical || horizontal;

if (any) transform.position += (new Vector3 (0f, speed, 0f) * Time.deltaTime);

if (up) projectileYSpeed = 5f;
if (right) projectileXSpeed = 5f;
if (down) projectileYSpeed = -5f;
if (left) projectileXSpeed = -5f;

if (any && !vertical) projectileYSpeed = 0;
if (any && !horizontal) projectileXSpeed = 0;