Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by goet · Apr 15, 2015 at 01:11 PM · c#rigidbodyfpsgetcomponent

Bullet doesn't go forward

It spawns (although I think the recoil thing doesn't work), but then it just stays in the same position.

 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
     public float recoil = 0.5f;
     public GameObject bullet;
 
     private int counter;
     void Start(){
         counter = 0;
     }
 
     void Update () {
 
         if (Input.GetButton ("Fire1")) {
             counter++;
             Instantiate (bullet, transform.position, transform.rotation);
             bullet.transform.Rotate (0, -recoil*counter, 0);
             bullet.GetComponent<Rigidbody> ().AddForce (transform.forward * 10000);
         } else
             counter = 0;
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by _joe_ · Apr 15, 2015 at 01:24 PM

Here you go, you've had several issues, you should use GetButtonDown instead of GetButton to get one call per click.

You need to instantiate and save the instantiated object in a variable and assign the rotate and add force to the variable (instead of the prefab reference which is not the actual object).

 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
     public float recoil = 0.5f;
     public GameObject bullet;
     
     private int counter;
     void Start(){
         counter = 0;
     }
     
     void Update () {
         
         if (Input.GetButtonDown ("Fire1")) {
             counter++;
             GameObject myBullet = Instantiate (bullet, transform.position, transform.rotation) as GameObject;
             myBullet.transform.Rotate (0, -recoil*counter, 0);
             myBullet.rigidbody.AddForce (Vector3.forward * 500);
         } else
             counter = 0;
     }
 }
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
avatar image
1

Answer by MitchWardle180 · Jun 03, 2015 at 01:14 PM

I would create a bullet script and place it on the prefab instantiated and control the bullet movement from the script. You could set a muzzle velocity variable and have it change based on which weapon you are using causing a gun to shoot faster/slower than another. Also if you use a bullet script it would also be easier to apply drop and air friction to the bullet.

  • WOULD AVOID USING 'rigidbody.AddForce()' FOR BULLETS BECAUSE IF THE BULLET MOVES MORE THAN THE SIZE OF THE OBJECT THE COLLISION DETECTION FOR IT WOULD BE TERRIBLE*

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
avatar image
0

Answer by wenbo · May 04, 2015 at 10:11 AM

In my idea,it's better not use AddForce function,but attach a script on the bullet,in the script's update function,use logic function to make the bullet fly.This is assignment of responsibility,which will make you logic more clear.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

RigidBody.MovePosition seems jerky in movement 0 Answers

Distribute terrain in zones 3 Answers

Don't allow the player to shoot while projectile is travelling 1 Answer

How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 1 Answer

Multiple Cars not working 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