I know this question may sound familiar but I cannot find any answers for this nor can, in my limited experience with unity, wrap my head around how to make this calculation.
Take a look at this project. How do I get the bullets to shoot toward the cursor all the time? Keep in mind that the character isnt necessarily in the middle of the screen.
I have to say, I didn't download the project. But from the code you posted, something along these lines should do the job. The trick is - to read the current distance from the camera (to the player's ship, or whatever is launching the projectile). Then convert the mouse position to a world position at the same distance away from the camera.
You can then use the normalized difference between these two points as the velocity - multiplied by whatever speed you like.
(untested code - for illustrative purposes, may contain errors, etc!)
var projectile: Rigidbody;
var projectileSpeed = 20;
function Update ()
{
if(Input.GetMouseButton (0) )
{
var instantiatedProjectile : Rigidbody = Instantiate(projectile,transform.position, transform.rotation);
// get current distance from camera
var distFromCam = Camera.main.transform.InverseTransformPosition(transform).z;
// get target in world space, at same distance from camera:
var targetScreenPos = Input.mousePosition;
targetScreenPos.z = distFromCam;
var targetPosition = Camera.main.ScreenToWorldPoint(targetScreenPos);
// calculate direction & velocity:
var targetDelta = (transform.position - targetPosition);
var launchVelocity = targetDelta.normalized * projectileSpeed;
instantiatedProjectile.velocity = launchVelocity;
}
}
(If all your objects are always at the same fixed distance away from the camera, you could optimise this code by having that as a constant value rather than looked-up using InverseTransformPosition)
I was poking around at the Camera class documentation the other day, and I believe it has built-in functions for turning a screen or viewport coordinate (e.g. a mouse x/y) into a world-space coordinate at a specified depth from camera, or better yet, a ray that works with the collision system.