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 Jhyatt · Feb 03, 2016 at 07:22 PM · gameobjectrigidbodyaddforceinstance5.3

[SOLVED] Cannot AddForce to instantiated game object.

Hi,

I am attempting to add force to the rigidbody of an instantiated game object.

Here is my code:

 void Update () {
     Shoot();
 }

 void Shoot()
 {
     float x = spawnPoint.transform.position.x;
     float y = spawnPoint.transform.position.y;
     float z = spawnPoint.transform.position.z;
     spawnVector = new Vector3(x, y, z);

     if (Input.GetMouseButtonDown(0))
     {
         bulletInstance = Instantiate(bullet, spawnVector, Quaternion.identity) as GameObject;
         bulletInstance.AddComponent<Rigidbody>();
         bulletRB = bulletInstance.GetComponent<Rigidbody>();
         bulletRB.AddForce(Vector3.forward * bullVel * Time.deltaTime);
     }


 }

Currently what occurs is a bullet is instantiated at the spawn point and simply falls to the ground. There appears to be no force being applied the bullet.

alt text

Does anyone have any input as to why the force is not being applied?

(Side note: the variable bullVel is equal to 50f)

Thank you for any input.

untitled.png (126.4 kB)
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
1
Best Answer

Answer by Jhyatt · Feb 04, 2016 at 06:06 AM

I found the solution to my issue!

Multiplying by Time.deltaTime was causing the force applied to the bullet to be so small that there appeared to be no force applied to it at all. I set my bullVel variable to a whopping 50,000f and the bullet moved as I expected.

I also found that using Vector3.forward caused my bullet to travel the same direction regardless of where my gun was facing. To fix that I used transform.forward instead.

Here is my working code:

     void Update () {
         Shoot();
     }
     void Shoot()
     {
         float xDir = spawnPoint.transform.position.x;
         float yDir = spawnPoint.transform.position.y;
         float zDir = spawnPoint.transform.position.z;
         spawnVector = new Vector3(xDir, yDir, zDir);
 
         if (Input.GetMouseButtonDown(0))
         {
             bulletInstance = Instantiate(bullet, spawnVector, Quaternion.identity) as GameObject;
             bulletInstance.AddComponent<Rigidbody>();
             bulletRB = bulletInstance.GetComponent<Rigidbody>();
             bulletRB.AddForce(transform.forward * bullVel);
         }
     }

EDIT --- Here is better code for bullet collisions!

If you are trying to do the same thing I am and you intend on your bullets colliding with objects I would encourage you to use a raycast instead.

What was happening that caused me to go with the raycast was my bullets were moving so fast that Unity would sometimes not detect the collisions and the bullets would fly straight through objects.

To prevent this I am using a raycast and detecting collisions with the ray and then instantiating my bullet directly on the point where the ray collided with the object. This ensures your bullet will collide with the object.

Here is my code for that:

 private RaycastHit hit;
     void Update () {
         Shoot();
     }
     void Shoot()
     {
         float xDir = spawnPoint.transform.position.x;
         float yDir = spawnPoint.transform.position.y;
         float zDir = spawnPoint.transform.position.z;
         spawnVector = new Vector3(xDir, yDir, zDir);
 
         forwardDir = transform.forward;
 
         if (Input.GetMouseButtonDown(0))
         {
             Ray aimRay = new Ray(transform.position, forwardDir);
             if (Physics.Raycast(aimRay, out hit))
             {
                 bulletInstance = Instantiate(bullet, hit.point, Quaternion.identity) as GameObject;
                 bulletInstance.AddComponent<Rigidbody>();
             }
         }
     }


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

48 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

Related Questions

Not able to add forces to a Rigid Body Game Object from a script attached to another Game object which is a Trigger. 0 Answers

Accessing the rigidbody of a cloned Object? 1 Answer

Later grab doesn't jump 0 Answers

Kinematic sphere doesn't move 1 Answer

Why does my character sometimes jump higher than other times? 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