- Home /
Why can't I shoot my bullet down?
I try to shoot some bullets at the direction the player is looking, it works fine as long as I look upwards, but when I look down the bullet just flys forward and not down. using UnityEngine; using System.Collections;
public class ShootObject : MonoBehaviour {
public Rigidbody body;
public GameObject camera;
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetKeyDown (KeyCode.Mouse0)) {
body = Instantiate(body, camera.transform.position, camera.transform.rotation) as Rigidbody;
body.AddForce(camera.transform.forward * speed);
}
}
}
Sorry for my bad english I'm from Germany
Answer by Chris333 · Jan 18, 2015 at 02:58 PM
Hi,
assuming that you are using the first-person-controller, it could be that the bullets are colliding with the capsule collider of the player when you are looking down. Move the camera of the first-person-controller a little bit forward or change the position of the instantiation of the bullet. You could also give the player and the bullet a specific layer and change the collision layers of the bullet under Edit/Project Settings/Physics by unchecking the checkmarks in the collision layer matrix. Than the physic system will not calculate any collision between these to objects. http://docs.unity3d.com/Manual/LayerBasedCollision.html
Additional, but that depends on your game, the usage of raycasts could make more sense, especially when you are making a ego-shooter or something like that.