Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Oct 06, 2017 at 11:28 PM by $$anonymous$$.
avatar image
0
Question by $$anonymous$$ · Apr 07, 2016 at 12:26 AM · c#speedbulletshoot

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);
 
 }
 
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Apr 08, 2016 at 10:50 PM 0
Share

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.

avatar image Fredex8 $$anonymous$$ · Apr 08, 2016 at 11:34 PM 0
Share

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.

avatar image
0

Answer by $$anonymous$$ · Apr 08, 2016 at 10:50 PM

@Fredex8

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

C# 2D Shooting a bullet based on player direction 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges