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 tadmakesgames · Nov 24, 2019 at 05:50 PM · rigidbodyinstantiateinstantiate prefabrigidbody.addforce

Instantiate and add Rigidbody force

Hey all, I'm trying to add force to a game object's rigidbody when it's instantiated. I'm pretty sure I'm just missing something simple here, but for some reason it's spawned and just falls to the ground from its spawn position. Any help would be greatly appreciated! Here's my code snippet, let me know if you need any more to diagnose the issue!

     public void ShootMe()
     {
         Vector3 playerDirection = player.forward;
         Quaternion playerRotation = player.rotation;
 
         Vector3 spawnPos = player.position + playerDirection * spawnDistance;
         Instantiate(item, spawnPos, playerRotation);
         item.GetComponent<Rigidbody>().AddForce(player.forward * multiplier);
 
         StartCoroutine(Reset());
     }

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 tadmakesgames · Nov 24, 2019 at 07:39 PM 0
Share

Also, "player" is a stored Transform.

avatar image tadmakesgames · Nov 24, 2019 at 07:44 PM 0
Share

Quick update:

I did some more Google searching and found a suggestion to instantiate the object differently. I tried it, but I'm getting identical results. Here's what I'm currently using:

     public void Shoot$$anonymous$$e()
     {
         Vector3 playerDirection = player.forward;
         Quaternion playerRotation = player.rotation;
 
         Vector3 spawnPos = player.position + playerDirection * spawnDistance;
         GameObject newItem = (GameObject)Instantiate(item, spawnPos, playerRotation) as GameObject;
         newItem.GetComponent<Rigidbody>().AddForce(player.forward * multiplier);
 
         StartCoroutine(Reset());
     }
 

avatar image tadmakesgames · Nov 24, 2019 at 09:07 PM 0
Share

$$anonymous$$ajor shoutout to @ABChief for trying to help me here. Just gonna leave this additional relevant info here:

I'm spawning a prefab that has a Rigidbody attached with Use Gravity ticked on.

3 Replies

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

Answer by ddulshan · Nov 24, 2019 at 09:23 PM

Make sure the multiplier value is high enough to put an affect on the object. Try changing the values until you get the desired effect. And you might want to look into force modes https://docs.unity3d.com/ScriptReference/ForceMode.html

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 tadmakesgames · Nov 24, 2019 at 09:29 PM 0
Share

Oh boy, do I feel dumb. I was incri$$anonymous$$ating multiplier as long as left mouse was held and then on left mouse up I was firing. I forgot that I set a max value and it was way too low. Thank you so much for this answer, I've been pulling my hair out all day.

Also, it wasn't a waste! The link you provided is gonna be super useful. Thanks again!

avatar image ddulshan tadmakesgames · Nov 25, 2019 at 07:13 AM 0
Share

Glad I could help, good luck!

avatar image
0

Answer by ABChief · Nov 24, 2019 at 08:24 PM

@tadmakesgames It's because there isn't a RigidBody attached

 GameObject newItem = Instantiate(new GameObject(), spawnPos, playerRotation);
 newItem.AddComponent<Rigidbody>();
 newItem.GetComponent<Rigidbody>().AddForce(player.forward * multiplier);
Comment
Add comment · Show 5 · 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 tadmakesgames · Nov 24, 2019 at 08:45 PM 0
Share

I'm spawning a prefab that does in fact have a rigidbody :/

avatar image ABChief tadmakesgames · Nov 24, 2019 at 08:51 PM 0
Share

In the RigidBody attached to the prefab is use gravity turned off?

avatar image tadmakesgames ABChief · Nov 24, 2019 at 08:53 PM 0
Share

No it's not, but I'm creating a physics based game and really need the gravity.

Show more comments
avatar image
0

Answer by Cornelis-de-Jager · Nov 24, 2019 at 09:28 PM

It should be as simple as:

 var spawn = Instiate(item, position, rotation);
 spawn.GetComponent<RigidBody>().AddForce(200, ForceMode.Impulse);
 
 
 

But I've found that when doing addForce it can get called before the physics of an insantiated object has been initialised. The fix is easy though.

 void Spawn () {
     var spawn = Instiate(item, position, rotation);
     StartCoroutine(PhysicsLate(spawn.GetComponent<RigidBody>()));
 }

 IEnumerator PhysicsLate (RigidBody rb){
     yield return new WaitForSeconds(0.01f);
     rb.AddForce(200, ForceMode.Impulse);
 }
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

179 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 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

Trouble with rb.Addforce(transform.foreward) 3 Answers

Rotation problem with spawning bullet 2 Answers

VR Sword Physics, how to add force or velocity to the sword for impact when it hits an object 1 Answer

OnCollisionEnter is slow... 2 Answers

Multiple Buildings Placement 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