How do I shoot a sphere from the center of my screen in the direction I am looking using FP Controller?

Hello everyone,

I have a GUI Texture that has a crosshair image which is constantly in the center of my screen. I want to make it so that when I click, it shoots a Rigidbody sphere from the center crosshair towards the direction I am looking. The current code is as follows:

    using UnityEngine;
    using System.Collections;
    
    public class ShootBallv2 : MonoBehaviour {
    	public Rigidbody projectile;
    
    
    	// Use this for initialization
    	void Start () {
    	
    	}
    	
    	// Update is called once per frame
    	void Update () {
    
    
    		if (Input.GetButtonDown("Fire1")) {
    		
    			Rigidbody clone;
    	
    			clone = (Rigidbody)Instantiate(projectile, Camera.main.transform.position, Camera.main.transform.rotation);
    				
    			clone.transform.rotation = Camera.main.transform.rotation;
    			clone.velocity = transform.TransformDirection (Camera.main.transform.forward * 20);
    			Physics.IgnoreCollision(clone.collider, collider);
    			Debug.Log ("Fire");
    		}
    		
    
    	}
    }

This acts very strangely though and seems to shoot the balls in random directions and does not shoot from the center anyways. Can someone please help? Thank you in advance for any help I can get.

Additional Info:
Script is attached to the First Person Controller

I figured it out, sorry about that. The issue appears to have been in the velocity equation. By removing “transform.TransformDirection” it appears to have fixed the issue. I have included my revised script below:

using UnityEngine;
using System.Collections;

public class ShootBallv2 : MonoBehaviour {
	public GameObject projectile;
	float bulletForce = 150f;
	float bulletSpeed = 20f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {


		if (Input.GetButtonDown("Fire1")) {
			Camera cam = Camera.main;
			GameObject theBullet = (GameObject)Instantiate(projectile,cam.transform.position, cam.transform.rotation);
			Physics.IgnoreCollision (theBullet.collider,collider);
			theBullet.rigidbody.velocity = cam.transform.forward * bulletSpeed;


		}
		

	}
}

If you just need a sphere you could attach this to the sphere…
(tested :wink:

{
	public float speed = 10;
	public float range = 10;

	private float dist;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update ()
	{
		transform.Translate(Vector3.forward*Time.deltaTime*speed);
		dist += Time.deltaTime*speed;
		if(dist>=range)
		{
			Destroy( gameObject );
		}
	}
}

And this to fire from the middle of the screen…

{
	public GameObject bullet;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update ()
	{
		if(Input.GetButtonDown("Fire1"))
		{
			Instantiate(bullet, this.transform.position, this.transform.rotation);
		}
	}
}

If you have issues with direction/fire location, add an empty game object to the player and position it where necessary. Just remember to orient the Z axis in the direction your player is facing.