How do I lock to a 45 degree Angle when projecting a bullet in the direction of a mouse click?

I have a script (C#) that will project bullet in the direction of where I click the left mouse button, What I want is to lock the shots to a 45 degree angle instead of directly towards the mouse click.

This is the code I have so far:

using UnityEngine;
using System.Collections;

public class ProjectileMouse : MonoBehaviour 
{

	void Start () 
	{
		Vector3 ScreenPoint = Camera.main.WorldToScreenPoint(transform.position);
		Vector3 direction = (Input.mousePosition - ScreenPoint).normalized;
		Debug.Log("Mouse Position: "+Input.mousePosition);
		rigidbody2D.AddForce (direction * 1000);
	}
}

If you still need help, I made a function that takes any V3 and turns the gameObject towards the nearest 45 degree step on the y axis. Maybe you can use the math part of the code for your own needs. Sorry, it’s in Unity Script (back then I still used it), but it should easily be transferred to C#.

function TurnTowards(LookAtTarget : Vector3) {	

	var aimingDirection = LookAtTarget - transform.position;
	var qTo : Quaternion;
	var angle = -Mathf.Atan2(aimingDirection.z, aimingDirection.x) * Mathf.Rad2Deg + 90.0f;
	angle = Mathf.Round(angle / 45.0f) * 45.0f;
	qTo = Quaternion.AngleAxis(angle, Vector3.up);

	transform.rotation = qTo;

}

You could try something like this. (This is probably not optimized, so if other people have improvements, feel free to share them :p)

    public int options = 8;

    private void OnDrawGizmos()
    {
        // Calculate the decrees in between the options
        float degrees = 360f / options;

        // Get mouse position relative to us
        Vector3 direction = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);

        // calculate the angle
        float angle = Mathf.Atan2((0 - direction.y), (0 - direction.x)) * Mathf.Rad2Deg + 90f;

        // Snap the rotation to our degrees
        angle = Mathf.Round(angle / degrees) * degrees;

        // Create a quaternion 
        Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);

        // Display our result
        Gizmos.DrawRay(transform.position, q * Vector3.up * 2f);
    }

Options is the amount of ‘directions’ that are allowed. (Couldn’t find a better name for it), so (4 == 90 degrees), (8 == 45 degrees), etc.

Ok try this :

void Start ()
	{
		Vector3 ScreenPoint = Camera.main.WorldToScreenPoint(transform.position);
		Vector3 direction = (Input.mousePosition - ScreenPoint);
		direction.z = 0f;
		direction = direction.normalized;
		
		if (direction.x <= -0.9) {
		
            direction = new Vector3( -1f, 0f, 0f);
            
        } else if ((direction.x > -0.9) && (direction.x <= -0.3)) {
		
            if (direction.y > 0) {
                direction = new Vector3( -0.7f, 0.7f, 0f);
            } else {
                direction = new Vector3( -0.7f, -0.7f, 0f);
            }
		
		} else if ((direction.x > -0.3) && (direction.x <= 0.3)) {
		
            if (direction.y > 0) {
                direction = new Vector3( 0f, 1f, 0f);
            } else {
                direction = new Vector3( 0f, -1f, 0f);
            }
			
		} else if ((direction.x > 0.3) && (direction.x <= 0.9)) {
		
            if (direction.y > 0) {
                direction = new Vector3( 0.7f, 0.7f, 0f);
            } else {
                direction = new Vector3( 0.7f, -0.7f, 0f);
            }
			
		} else if (direction.x > 0.9) {
		
            direction = new Vector3( 1f, 0f, 0f);
        }
		
		rigidbody2D.AddForce (direction * 1000);
	}