# AddForce Problem ? \[ Solved \]

**URL:** https://discussions.unity.com/t/addforce-problem-solved/417952
**Category:** Physics
**Tags:** Scripting
**Created:** [July 12, 2010, 9:35pm UTC](https://discussions.unity.com/t/addforce-problem-solved/417952 "2010-07-12T21:35:22Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![T-Pr](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@T-Pr](https://discussions.unity.com/u/T-Pr)
#### Post date: [July 12, 2010, 9:35pm UTC](https://discussions.unity.com/t/addforce-problem-solved/417952/1 "2010-07-12T21:35:22Z")

</div>

Hi guys , i have a very easy problem about AddForce in Java Script. I did this a long time ago but i couldn’t remember how i did this. So , i want to make a shoot script . I wrote those ;

```auto
var mermiPrefab : GameObject;

function Update () 

{
    if (Input.GetButtonDown("Fire1") )
	{
	 Instantiate(mermiPrefab,transform.position,Quaternion.identity);
	 mermiPrefab.rigidbody.AddForce(transform.forward * 2000);
	}

}

```

When i click mouse 1 , mermiPrefab ( it’s bullet prefab ) appears but it’s not going forward , i put rigidbody in it , again it appears but this time it falls down. I mean AddForce didn’t work , is there any mistake about it ?

---

<div class="post-metadata">

### Author: ![dart](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dart/32/243762_2.png) [@dart](https://discussions.unity.com/u/dart)
#### Post date: [July 12, 2010, 9:42pm UTC](https://discussions.unity.com/t/addforce-problem-solved/417952/2 "2010-07-12T21:42:00Z")

</div>

Rigidbody.AddForce has to be applied every frame to work. You can set the velocity of the rigidbody to some initial value and the physics engine will handle the evolution of the movement.

---

<div class="post-metadata">

### Author: ![anon\_51720730](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon\_51720730](https://discussions.unity.com/u/anon_51720730)
#### Post date: [July 12, 2010, 9:42pm UTC](https://discussions.unity.com/t/addforce-problem-solved/417952/3 "2010-07-12T21:42:31Z")

</div>

You need to save the result of Instantiate to a variable, then use that variable (and not the prefab).

For example:

```auto
[...]
    var newBullet = Instantiate(mermiPrefab,transform.position,Quaternion.identity); 
    newBullet.rigidbody.AddForce(transform.forward * 2000); 
[...]

```

---

<div class="post-metadata">

### Author: ![T-Pr](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@T-Pr](https://discussions.unity.com/u/T-Pr)
#### Post date: [July 13, 2010, 9:43am UTC](https://discussions.unity.com/t/addforce-problem-solved/417952/4 "2010-07-13T09:43:27Z")

</div>

Thanks a lot guys 🙂 , i solved the problem,thank you again :).

---

<div class="post-metadata">

### Author: ![raviraj\_vdy](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@raviraj\_vdy](https://discussions.unity.com/u/raviraj_vdy)
#### Post date: [August 11, 2010, 6:51am UTC](https://discussions.unity.com/t/addforce-problem-solved/417952/5 "2010-08-11T06:51:11Z")

</div>

Hi Guys

Iam facing the same issues only im using “C sharp” as my programming language following is the code im using:

public class Fire\_Bullet : MonoBehaviour {

public Rigidbody bullet;

// Use this for initialization  
void Start () {

}

// Update is called once per frame  
void Update () {  
shootbullet();  
}  
void shootbullet(){  
Rigidbody Clone;  
if(Input.GetMouseButtonDown(0)){  
Clone = (Rigidbody) Instantiate (bullet, this.transform.position, this.transform.rotation);  
Clone.rigidbody.AddForce(0,0,1000);  
}  
}  
}

OK so the problem is the the bullet gets Instantiate how ever it does not go in the direction i want it to go in please help.

Also after the object is Instantiate when I set it to get destroyed after say 2sec it does not get destroyed, I am using the following code to destroy the Instantiate object

“Destroy(Clone, 2);”

NOTE: I am new to Unity programming.

---

<div class="post-metadata">

### Author: ![anon\_51720730](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon\_51720730](https://discussions.unity.com/u/anon_51720730)
#### Post date: [August 11, 2010, 12:34pm UTC](https://discussions.unity.com/t/addforce-problem-solved/417952/6 "2010-08-11T12:34:22Z")

</div>

You are instantiating the bullet with the same orientation as the transform.

So, make sure the object in which that script is on, is pointing in the direction you want the bullet to go.

Or change the “this.transform.rotation” in the Instantiate for a rotation aiming at where you want.

---

<div class="post-metadata">

### Author: ![raviraj\_vdy](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@raviraj\_vdy](https://discussions.unity.com/u/raviraj_vdy)
#### Post date: [August 11, 2010, 12:56pm UTC](https://discussions.unity.com/t/addforce-problem-solved/417952/7 "2010-08-11T12:56:46Z")

</div>

OK but if its not possible to rotate the object then what should be done I mean how should I define the rotation that the instantiated object should take ?

---

<div class="post-metadata">

### Author: ![anon\_51720730](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon\_51720730](https://discussions.unity.com/u/anon_51720730)
#### Post date: [August 11, 2010, 1:49pm UTC](https://discussions.unity.com/t/addforce-problem-solved/417952/8 "2010-08-11T13:49:31Z")

</div>

Something like this:

> [@](#):
>
> void shootbullet() {
> 
> // this defines the rotation  
> // replace x, y and z with the rotation you need in Euler angles  
> Quaternion rot = Quaternion.Euler(x, y, z);
> 
> Rigidbody Clone;  
> if(Input.GetMouseButtonDown(0)){  
> Clone = (Rigidbody) Instantiate (bullet, this.transform.position, rot);  
> Clone.rigidbody.AddForce(0,0,1000);  
> }  
> }

---

<div class="post-metadata">

### Author: ![raviraj\_vdy](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@raviraj\_vdy](https://discussions.unity.com/u/raviraj_vdy)
#### Post date: [August 11, 2010, 2:42pm UTC](https://discussions.unity.com/t/addforce-problem-solved/417952/9 "2010-08-11T14:42:30Z")

</div>

Hey thanks for helping ,I will try it out n let u know 😄
