I created two bullet scripts and two bullet prefabs. I have questions about why each behaves the way they do. My first bullet type uses gravity and moves with .AddForce, while my second bullet called MagicBullet does not use gravity, and moves with transform.position.
ShootBullet.cs:
public GameObject bullet;
public float speed = 100f;
void Start() { }
void Update() {
if (Input.GetMouseButtonDown(0))
Fire();
}
void Fire() {
Vector3 forward = transform.TransformDirection(Vector3.forward);
GameObject instBullet = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
Rigidbody instBulletRigidBody = instBullet.GetComponent();
instBulletRigidBody.AddForce(forward * speed);
}
Question 1: With the above script attached to my "emitter" empty object located at the muzzle of my gun object, weird things happen when I look up and down. It seems that the degree by which it aims is doubled. So if I aim at 0º forward, parallel to the ground, it fires just fine. If I look up at 45º, it fires at 90º straight up instead. You can see this [here][1] and [here][2]. Right hand fires black regular bullets, left hand fires blue magic bullets.
My magic bullet firing script is identical except that it does _not_ have the .AddForce call. Instead it has this in the MagicBullet.cs that is attached to the magic bullet prefab:
void Update() {
// transform.Translate(transform.forward * speed * Time.deltaTime);
transform.position += transform.forward * speed * Time.deltaTime;
}
Question 2: transform.position seems to work great and does not have any issues so far. What I had been using, and was not working, was transform.Translate. What it does is similar to the first issue above, except it applies not just to looking vertically but horizontally as well as you can see [here][3]. Little hard to see, but look for the blue bullets not going the direction the player is facing. If up is 0º and right is 90º, it seems that at 45º the blue bullets fire at 90º, and at 90º they fire at the full 180º. Now, this is fixed by using transform.pos instead of .Translate, but **I want to understand why**.
Final questions: Am I doing this a "good and proper" way or should I be doing bullets totally differently than I am? And if so, can you link me a tutorial or something?
[1]: https://puu.sh/GXDqg/9c7fdfcdd1.mp4
[2]: https://puu.sh/GXCVm/022747b1eb.mp4
[3]: https://puu.sh/GXDh1/a67e708872.mp4
↧