Rigidbody.addForce is not working
hey I am trying to make my character fire a bullit, i use this code to instantiate the bullit and add force to it! but some how the bullit is not getting move by the force!
if(Input.GetButtonDown ("Fire2"))
{
var bullit = Instantiate (bullitPrefab, gameObject.FindWithTag ("spawnPoint").transform.position,gameObject.FindWithTag("spawnPoint").transform.rotation);
bullit.rigidbody.AddForce(transform.forward * 200);
}
can u guys help me here?
Answer by Nilesh · Feb 20, 2012 at 12:36 PM
Try bullit.rigidbody.AddForce(transform.forward * 200,ForceMode.Impulse);
i know, this is a couple of years ago. but i came here via google, and your Answer solved all my problems today! thanks.
7 years later, answer still valid. Who knew we needed to specify the ForceMode... Thanks!
Answer by denewbie · Nov 06, 2010 at 04:14 PM
Does your "Gun" have a collider? If it does then the bullet collide with your "Gun" before it can ever fire out right...
It has a collider on it, but the spawn point of the bullet is far enough away from it. and shouldn't the bullet then at least move as soon as i move away from it??
In that case may i just check of you that the bullet was spawned and not moving or it was never spawned at all?
Answer by Loius · Nov 05, 2010 at 02:46 PM
Your bull*E*t object is missing a rigidbody or its rigidbody is kinematic. If that's not the problem you'll have to specify exactly what does happen when you press "Fire2".
ok i wrote bullet wrong, sorry about that :P but what do u mean by specify, the code should be right but it doesn't work.. so where is the mistake??
I know this is many years old, but what he meant is what happens when you press it, does it shoot you, does it not shoot, does it teleport somewhere.(just incase you need it for future)
Answer by pjnovas · Sep 05, 2014 at 05:39 PM
I know this post is really old, but I came across the same problem and I realised the transform.forward is a Vector3.zero, it is at (0,0,0) because of your rotation.
So if you do a Vector3.zero * 200 is a Vector3.zero and adding a force of ZERO will do nothing.
I'm leaving this if someone else has the same thing.
Answer by WARW0LF · Dec 18, 2020 at 01:33 PM
I found this way works the best:
Vector3 direction = Vector3.zero;
direction = Vector3.forward * amtOfForce;
rigidbody.AddForce(direction * Time.fixedDeltaTime, ForceMode.Acceleration);
use transform.forward if vector3.forward isn't right, your amtOfForce has to be really high, just for a jump i had to set my force float to 15000, this should be placed in a FixedUpdate if posible and always used Time.fixedDeltaTime for physics
hope this helps someone else wondering why addforce doesn't work