Fire bullet in camera direction problem

So I’ve been at it for 3 hours and have made no progress. For some reason my bullets don’t properly respond to the Y axis of my camera. Image below and my code to help explain.

alt text

	void shoot() {
		NetworkViewID viewId = Network.AllocateViewID ();
		networkView.RPC ("spawnBullet", RPCMode.AllBuffered, viewId, Camera.main.transform.position, Camera.main.transform.rotation);
	}


	[RPC]
	void spawnBullet(NetworkViewID viewId, Vector3 bulletPos, Quaternion bulletRot) {
		bullet = Resources.Load ("bullet");
		GameObject myBullet = (GameObject)Instantiate(bullet,bulletPos,bulletRot); 
		NetworkView nView = myBullet.GetComponent<NetworkView> ();
		nView.viewID = viewId;
		print ("bulFor: "+myBullet.transform.forward);
		myBullet.rigidbody.velocity = myBullet.transform.forward * 80f;
	}

Hi,

You are using ‘bulletPos’ as a transform here, are you sure that this position is assigned to the correct place?

The way I personally would go about implementing this, would simply be to use Camera.main (assuming you are using the main camera) or playerCam (some camera variable that points to your players camera) transform.position & transform.rotation.

This should in theory mean that it would be instantiated exactly in the middle of the camera, which is where you are aiming from…

Hope that helps :slight_smile:

Good luck,

IEMatrixGuy

bulletPos & bulletRot are “Camera.main.transform.position, Camera.main.transform.rotation” as defined in the network rpc call.

I found an answer though. The bullets are travelling too fast for the physics engine. I had to up the timestep significantly for it to work. So will need to take a look at the performance hit and work out whether to work out another way or not.

Whats your bullet force, and if it is travelling too fast, you may want to use the Check collision script found on unity wiki, allows you to check rigidbody collisions using a script and works perfectly with huge forces