- Home /
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.
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;
}
Answer by IEMatrixGuy · Nov 13, 2014 at 05:35 AM
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 :)
Good luck,
IEMatrixGuy
Answer by ryanjuk · Nov 13, 2014 at 06:50 AM
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.
Answer by AngryBurritoCoder · Nov 13, 2014 at 11:25 AM
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
Your answer
Follow this Question
Related Questions
Everytime I Instantiate, object becomes smaller. 2 Answers
bullet issue 3 Answers
Bullet stops when new bullet instantiates pls help 3 Answers
Modify the rotation of Instatiate prefab 1 Answer
Best way to make bullets? 2 Answers