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 /
avatar image
0
Question by Xerunix · Apr 22, 2016 at 05:26 AM · rigidbodyinstantiateforce

Rigidbody bullet force not being applied

Sorry in advance, because I know this has been asked before, but I can not figure out the issue for the life of me. I have followed the unity scripting tutorial for instantiate as best I can and it just isn't working.

I am just trying to fire a cannonball from the end of a cylinder.

public class ShootTurret : MonoBehaviour { public GameObject cannonballPrefab; public Transform turretEnd; public float fireRate = 1.0F; private float nextFire = 0.0F;

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space) && Time.time > nextFire)
     {
         nextFire = Time.time + fireRate;
         Rigidbody cannonballInstance;
         cannonballInstance = Instantiate(cannonballPrefab, turretEnd.position, turretEnd.rotation) as Rigidbody;
         cannonballInstance.AddForce(new Vector3(0f,0f,-100000f), ForceMode.Impulse);
     }
 }

I am trying to fire it in the -z axis even though that is normally back. Does anyone see why my bullets are just dropping straight down?

Comment
Add comment · Show 3
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 toromano · Apr 22, 2016 at 05:40 AM 0
Share

Things you can check: 1- Check if ball is colliding with the turret when it is instantiated. 2- You are applying a static global force to the ball. you need to apply force in turrets forward direction (might be a different axis if you are using a *.fbx file) :

 cannonballInstance.AddForce(100000f * turretEnd.forward, Force$$anonymous$$ode.Impulse);

Hope that helps.

avatar image Xerunix toromano · Apr 22, 2016 at 07:15 PM 0
Share

I have an empty game object in front of the turret and that is what the turretEnd variable is being set to so there shouldn't be any collisions upon instantiation. I have rotated the empty gameobject so the +z points in the direction I want the bullet to go in. Here is a picture of the setup. http://imgur.com/dd7vXak. The ball is still dropping directly in front of the cannon upon instantiation. I am also getting an "Object Reference not set to an instance of an object" error on the cannonballInstance.AddForce line, but I don't see why.

avatar image Xerunix toromano · Apr 22, 2016 at 07:20 PM 0
Share

Solution: The cannonballInstance is a GameObject not just a rigidbody. The way to fix this was to instantiate as GameObject, get the rigidbody component from the GameObject, then apply the force to the rigidbody. Here is a pastebin. http://pastebin.com/0NUjg7TE

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Creating a rope in 3D for grappling 0 Answers

How to make rigidbody not effect movement while still using it for collisions? 0 Answers

Instant rotation bug when rotating clockwise 0 Answers

Addforce function not adding force in a 2D game and cannot figure out why 2 Answers

Script removing rigidbody component, NOT game object 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