Shoot at mouse pos 2D scroll

var crosshairTexture : Texture2D;
var prefabBullet:Transform;
var shootForce:float;
var projectile: Rigidbody;
var speed = 20;

Screen.showCursor = false;

function OnGUI () {
    var mousePos = Event.current.mousePosition;
    GUI.DrawTexture( Rect( mousePos.x - (crosshairTexture.width/2),                        mousePos.y - (crosshairTexture.height/2),crosshairTexture.width,                         crosshairTexture.height), crosshairTexture);
}


function Update () 
{
    var mousePos = Event.current.mousePosition;

if(Input.GetMouseButton (0) )
    {
        var instantiatedProjectile : Rigidbody = Instantiate(projectile,transform.position, transform.rotation); 

        instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (mousePos.x,mousePos.y, 0));
    }
}

Already asked this previously - hope this isnt against the site rules but I thought Id get a few opinions - im rather stumped here.

Im attempting to have a 2d shooter. The character should be able to shoot by mouse press and a projectile shoot towards the current mouse x&y.

At present with the code above, the mouse x and y vector points are found. Its attached to a GUITexture cross hair. It should shoot to the mouse x and y and 0 on z axis however, it isnt doing so :confused:

Please could you point me in the right direction with this (no pun intended really :P)

Cheers

What is the projectile behavior right now? does it instantiate and remain static?

There are a couple of things you should test here:

  1. GUI element position is in screen space, your game is in world space, you should translate the mouse coordinates using Camera.ScreenToWorldPoint
  2. You never set a starting velocity for your projectile. Try multiplying the transformDirection vector by a constant velocity to get your projectiles moving.
  3. If you are using rigidbodies to simulate projectile movement, you should get them moving by applying a force in the direction of movement, and not altering the velocity directly - see Rigidbody.AddForce