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 /
avatar image
0
Question by ak2600 · May 18, 2018 at 06:30 PM · shootingbulletgun

Bullet does not move forward after being initiated

I have two separate scripts. One that allows the enemy to fire bullet and one that detects bullet's collision with the player. The first one is applied on the enemy while 2nd on the Bullet prefab. When i use only the first script, the bullet behaves like a rigid body, moves forwards, collides with objects and gets deflected etc. However, after applying the 2nd one, the bullet just doesn't move forward at all and stays at its position until it gets destroyed. Please help me resolve this issue. Thanks in advance.

Script 1: (has more functions too, but i have shown just one)

     void attack()
         {
             if (Time.time > nextFire) {
                 //shoot
                 GameObject tempBullet=Instantiate(bullet,gun.position,gun.rotation) as GameObject;
                 Rigidbody tempRigidBodyBullet = tempBullet.GetComponent<Rigidbody> ();
                 tempRigidBodyBullet.AddForce (tempRigidBodyBullet.transform.forward * bulletspeed);
                 Destroy (tempBullet, 0.30f);
                 //play audio
                 bulletAudio_player.Play();
                 nextFire = Time.time + fireRate;
             }
         }
 

Script 2:(it has only one function)

 void OnCollisionEnter(Collision col)
     {
         Destroy (gameObject);
 
         if (col.gameObject.CompareTag ("Player")) 
         {
             PlayerHealth.health -= 2;
         }
     }
 



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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kciwsolb · May 18, 2018 at 07:46 PM

Hi, first of all, make sure you format all of your code as code. This was hard to read. Now, when you declare and initialize tempRigidBodyBullet you are doing this:

 Rigidbody tempRigidBodyBullet = tempBullet.GetComponent (); 

You are not specifying a Component Type. Use angle brackets after GetComponent and before your parameter list to specifiy a Type.

 Rigidbody tempRigidBodyBullet = tempBullet.GetComponent<Rigidbody>(); 


Comment
Add comment · Show 3 · 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 ak2600 · May 19, 2018 at 04:13 AM 0
Share

Hi. First, thanks for replying. Sorry for the issue due to improper formatting, but I am new to unity and did not know about code sample option but i have edited it now. Sir what you said was already in my code ( this i mean) but due to some reason was not shown in the question. Even after that its not working. What shall i do next? Thanks

avatar image Kciwsolb · May 20, 2018 at 05:02 AM 0
Share

Where are you initializing bulletSpeed? If it is somehow 0 in your code or the inspector, that could cause it.

avatar image ak2600 Kciwsolb · May 21, 2018 at 07:37 AM 0
Share

No it is not 0. It is moving perfectly before applying the second script

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 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

Related Questions

Help with getting a bullet to move. 2 Answers

Bullet does not move forward 1 Answer

Control amount of bullets 2 Answers

Shooting & Animation Problem 1 Answer

I Want To Shoot Bullets Correctly. 2 Answers


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