- Home /
Broken Script Help
Hello I'm a little new to Unity im trying to make my first Unity based project but the console says "No appropriate version of 'UnityEngine.Rigidbody.AddForce' for the argument list '(float)' was found." I'm just trying to add force to my bullet to make it actually shoot. Here script:
function Update(){
if(Input.GetKeyDown("space")){
Instantiate(bullet, GameObject.Find("Bullet Spawn").transform.position, Quaternion.identity);
bullet.rigidbody.AddForce(transform.position.z * 2000);
print("shot");
}
}
Thanks!
Answer by Statement · Dec 10, 2010 at 11:03 PM
No appropriate version of 'UnityEngine.Rigidbody.AddForce' for the argument list '(float)' was found.
bullet.rigidbody.AddForce(transform.position.z * 2000);
You are accessing the z component of position, which is a float value. I guess what you really want to do is get the forward direction of the transform. Also, you're trying to add the force to what looks to be the prefab. You should add the force to the new clone you instantiated.
var power = 2000;
var position = GameObject.Find("Bullet Spawn").transform.position;
var clone = Instantiate(bullet, position, Quaternion.identity);
clone.rigidbody.AddForce(transform.forward * power);
However, you might want to use another force mode since you're "shooting" the object away. Impulse force mode sounds appropriate.
This mode is useful for applying forces that happen instantly, such as forces from explosions or collisions.
var power = 2000;
var position = GameObject.Find("Bullet Spawn").transform.position;
var clone = Instantiate(bullet, position, Quaternion.identity);
clone.rigidbody.AddForce(transform.forward * power, ForceMode.Impulse);
You probably want to tweak power a bit. 2000 sounds a lot.
Thanks but I'v tried that and nothing happens the bullet just falls with no force at all.
I have updated my answer. We were trying to add the force on the prefab rather on the new bullet. :)
Ok so now when I shoot my bullet shoots on the wrong axis. I replaced AddForce(transform.forward... with AddForce(transform.position.x... and I get an error :( why doesnt that work?
It sounds like your character isn't aligned with the face pointing forward. transform.forward is just the forward direction (z axis, the the blue arrow) of your character. If your character is pointing another direction, use that direction. You can try transform.right or transform.up also, as well as trying to invert the direction with - (negate sign). Basically you just need the direction you're facing. You can get this direction from helper objects positioned to align the bullet if you have awkward angles but I doubt that is necessary.