- Home /
Rigidbody.AddForce not working
First of all: upon searching I found a lot of similar questions but none seemed to apply. The object (cannonball I'll call it cb) is a rigidbody, non-kinematic and is spawn outside of the cannon at an empty object to specify location.
I'm relatively new to scripting and didn't want to bother anyone but I spend several hours the past two days trying to fix and I can't =\
What happens is that the cannon rotates along it's base to follow the player around and always aim at him. Once every second it will instantiate a prefab named Cannonball. I try to apply a force/velocity to this cannonball along the forward/z axis (I've checked, the cb is instantiated with all axis as I'd want them, y upwards and z pointing at player ignoring height.) The applied force nor the velocity (I tried both) seems to work though, the cb just spawns and slowly by gravity falls to the ground and rolls off the hill. I tried huge values for AddForce as well, won't work.
Script is:
if(shouldIShoot) { var cb = Instantiate(bulletPrefab, transform.Find("CannonSpawn").transform.position, Quaternion.identity);
cb.Rigidbody.AddForce(transform.forward * 3000000);
shouldIShoot = false;}
Help would be greatly appreciated, preferably in the form of a concrete example of how it should look like, because I'm still new to js :)
Answer by kkaploon · Feb 20, 2011 at 09:57 PM
Try this:
cb.rigidbody.AddForce(transform.forward * 30);
Rigidbody is the class, rigidbody is the instance of it, and that's what you want to change.
Thank you so much, you have no idea how much you've helped me! This was driving me absolutely insane to the point of ragequiting on my project xD. I feel like such a fool now.
Small bonus question: Is there anything else I can do to credit you for this other then clicking the (^) button? :)
Don't feel like a fool, I had this kind of thing happen to me more than I could count. Happens to everybody.