- Home /
fps shooting in the direction of character main cam
hi im having trouble shooting in my fps game the script i am using is below. the script allows me to fire horizontally but it doesnt take into account the angle the cam is so if i look up or down the bullet still fires along the horizontal plane. any suggestions?
//player 1 shoot
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
var bullit =Instantiate(bullitPrefab,
GameObject.Find("Main Camera").transform.position,
transform.rotation);
bullit.rigidbody.AddForce(transform.forward * 4000);
}
}
i needed to add a spawn point and make it a child of the character controller i found this helpful
http://answers.unity3d.com/questions/237436/shooting-fail-due-to-camera-setup.html
Answer by aldonaletto · May 20, 2012 at 04:15 AM
The easiest way is to create an empty game object (the spawn point) and child it to the camera. Adjust the position and rotation of this empty object: the Z axis (blue) must point in the same direction as the camera, and its position must be 1 to 2 units ahead of the player (to not collide with it). Add the shooting script below to this game object:
var bullitPrefab: Transform;
function Update() { if(Input.GetButtonDown("Fire1")) { var bullit =Instantiate(bullitPrefab, transform.position, transform.rotation); bullit.rigidbody.AddForce(transform.forward * 4000); } } Since the spawn point object is childed to the camera, it will point wherever the camera points.
This worked for me. Except I just planted the script into a child of the camera.
Your answer
Follow this Question
Related Questions
Player cannot shoot 2 Answers
Shooting only once, returning boolean as false 2 Answers
Gun Firing help? 2 Answers
Need help... Start Button no longer working!!!!! 0 Answers
Help with fire script 1 Answer