Can't shoot towards mouse click point

I’m making 3d space game from side-view, and i wanted to add some weapons that would shoot on x and y axis towards the point where the player click the mouse.I found something so i tried this:

var hit  : RaycastHit ;
	if(Input .GetButtonDown ("Fire1"))
	{
	 	var ray : Ray  = Camera .main .ScreenPointToRay (Input .mousePosition );
	 	if(Physics.Raycast (ray, hit, Mathf.Infinity))
	 	{
	 		var shootProjectile : Rigidbody = Instantiate(projectile, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
	 		shootProjectile.AddForce (hit.point * projectileSpeed);
	 	}

	} 
}

It shoots well if i click in to empty space, but when i click on object with collider, it shoots in very weird directions…I spend day and half finding a solution, but I didn’t find it. I tried many things, just do’nt work. Can anyone help me? The best would be to post repaired code and explain why my code don’t work :slight_smile:

Okay, I fixed it!

var projectile : Rigidbody;
var projectileSpeed : float;
var spawnPt : GameObject;

function Start()
{
   spawnPt = GameObject.Find("spawnPoint");
}

function Update()
{
var hit  : RaycastHit ;
	if(Input .GetButtonDown ("Fire1"))
	{
	 	var ray : Ray  = Camera .main .ScreenPointToRay (Input .mousePosition );
	 	if(Physics.Raycast (ray, hit, Mathf.Infinity))
	 	{
	 		
	 		projectile = Instantiate(projectile, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
	 		projectile.AddForce(( hit.point - spawnPt.transform.position )*projectileSpeed);
	 	}
	}
}

function FixedUpdate()
{
	var velocity = projectile.velocity;
    if (velocity == Vector3.zero) return;

    var magnitude = velocity.magnitude;
    if (magnitude > projectileSpeed || magnitude < projectileSpeed)
    {
        velocity *= (projectileSpeed / magnitude);
        projectile.velocity = velocity;
    }
}

I’m so happy! :smiley: Who have the same problem, you can use this code :slight_smile:
thx for help AlucardJ :slight_smile: