- Home /
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());
}
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());
}
$$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.
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
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!
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);
I'm spawning a prefab that does in fact have a rigidbody :/
In the RigidBody attached to the prefab is use gravity turned off?
No it's not, but I'm creating a physics based game and really need the gravity.
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);
}