First problem is that you are applying the AddForce to the prefab rather than the instantiated version of it. Use this instead:
Rigidbody bulletInstance = Instantiate (bullet, coords.transform.position, bullet.transform.rotation) as Rigidbody;
You can then just AddForce directly to the bulletInstance without a GetComponent.
Next problem is that you aren’t telling it to go forward. You’ve actually got the right code for it print (bullet.transform.forward * bulletSpeed); but for some reason you’ve got that in a debug not the AddForce.
The figures you are using AddForce (new Vector3(0,0,44444) and bulletSpeed = 50000000000000f; are having to be so high because you are multiplying by Time.deltaTime which is going to be a very small decimal. Remove that and you can use more sensible figures.
Reduce the value of bulletSpeed to around 1000-5000 or so and then use something like this:
Depending on which way your gun and bullet models are facing in the prefabs you may need to change forward to up or right etc so that it actually flies forward relative to the model.
It doesn’t work, now the script is just shooting 2 bullets at once and they aren’t flying in the air. As soon as I press the button to fire the bullets drop straight to the ground.
using UnityEngine;
using System.Collections;
public class PlayerScriptThree : MonoBehaviour {
public GameObject bullet;
public GameObject coords;
public float bulletSpeed;
public Color[] cols = new Color[10];
public GameObject[] gObjs = new GameObject[11];
// Use this for initialization
void Start () {
bulletSpeed = 2500f;
print (bullet.transform.forward.ToString ());
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.KeypadEnter)) {
Shoot ();
}
}
void OnTriggerEnter(Collider _other){
if (_other.gameObject.tag == "JumpPower") {
this.gameObject.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().m_JumpSpeed = 30f;
//print (player.gameObject.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().m_JumpSpeed);
}
}
void Shoot(){
Instantiate (bullet, coords.transform.position, bullet.transform.rotation);
Rigidbody bulletInstance = Instantiate (bullet, coords.transform.position, bullet.transform.rotation) as Rigidbody;
print (bullet.transform.forward * bulletSpeed);
bulletInstance.AddForce(transform.forward * bulletSpeed);
}
}