Bullet spawn point confusion

This might be a tricky one.

-I’ve got a character with a gun
-When I hit V I can switch between first and a 3rd person/over the shoulder view.
-I’ve got the crosshair and camera moving fine
-I’ve got an empty object at the tip of the gun where bullets are instantiated from.

My problem is, I can’t figure out a good way to keep this empty object pointed at the center of the screen, ie keep the bullets traveling to where the crosshair is aiming.

I tried putting a smoothLookAt script on the empty, and jack the damping up, and make the crosshair the target, but that only works if the enemy happens to be the exact distance from the gun as the crosshair.

I tried putting a smoothLookAt script on the gun, and parenting the empty to the gun. This sort of worked, the bullets would travel to the center of the screen, but they’d only reach the center of the screen at a vast distance. Shooting wide at a short or even normal range.

Does anyone have any suggestions? I have it setup in such a way I can try anything pretty quickly, so suggest anything you can think of please. :]

[EDIT]I’m thinking now maybe what I need to do is fire a Ray from the centre of the camera, and point the empty object at the hit point. But I have no idea how on earth I’d accomplish this. Anyone got any ideas?[EDIT]

Get the Dastardly Bananas’ Gun project (just search it in Google). Then work from there :wink:

Right, I solved it, here’s the code for anyone who may happen across this.

//Point Bullets are insantiated at
public var bulletPoint : GameObject;

//Main Camera Object
public var cam : GameObject;

//Empty Object the bullet spawn point aims at
public var aimer : Transform;

//The Farthest distance the aimer will function
public var aimMaxDist : int = 100; 

/////////////////////////

function Update () 
{
	var fwd = cam.transform.TransformDirection(Vector3.forward);
	var hit : RaycastHit;
	var hitPoint : Vector3;

	Physics.Raycast(cam.transform.position, fwd, hit, aimMaxDist);
	hitpoint = hit.point;

	aimer.transform.position = hitpoint;
	transform.LookAt(aimer);
}

Parent an object (I used a blue sphere) with no collider on it to the aimer object to visually check the aimer is in the right place at all times.

Cheers for the tip on the Dastardly Banana’s tutorial man, that’ll come in handy for me. :]