My bullet won't go forward? Please check my script and advise me
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 = 50000000000000f;
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);
bullet.GetComponent<Rigidbody> ().AddForce (new Vector3(0,0,44444) * bulletSpeed * Time.deltaTime, ForceMode.VelocityChange);
print (bullet.transform.forward * bulletSpeed);
}
}
Answer by Fredex8 · Apr 07, 2016 at 01:21 PM
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:
bulletInstance.AddForce(transform.forward * bulletSpeed);
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 : $$anonymous$$onoBehaviour {
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.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$eypadEnter)) {
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);
}
}
I added this based on what you told me to put.
You need to remove:
Instantiate (bullet, coords.transform.position, bullet.transform.rotation);
as this replaces it:
Rigidbody bulletInstance = Instantiate (bullet, coords.transform.position, bullet.transform.rotation) as Rigidbody;
No force is being added (and it should be giving an error) because your bullet is a GameObject and not a Rigidbody. You need to change public GameObject bullet;
to public Rigidbody bullet;
and then reassign the reference in the inspector.
You should also remove the print commands as they are trying to access prefabs that don't exist in the scene so may cause an error.
Answer by $$anonymous$$ · Apr 08, 2016 at 10:50 PM
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);
}
}
I added this based on what you told me to put.
Follow this Question
Related Questions
NEED HELP PLEASE! Unexpected symbol. 0 Answers
Constant fire at target 2 Answers
Move enemy to a specific movepoint with an array (C#) 1 Answer
Setting up a gear system of sorts... 1 Answer