x


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!

more ▼

asked Dec 10 '10 at 10:57 PM

MrSplosion gravatar image

MrSplosion
132 45 52 61

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Dec 10 '10 at 11:03 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

Thanks but I'v tried that and nothing happens the bullet just falls with no force at all.

Dec 10 '10 at 11:11 PM MrSplosion

I have updated my answer. We were trying to add the force on the prefab rather on the new bullet. :)

Dec 10 '10 at 11:36 PM Statement ♦♦

Oh ok I see thanks

Dec 11 '10 at 03:52 AM MrSplosion

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?

Dec 14 '10 at 01:03 AM MrSplosion

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.

Dec 15 '10 at 01:40 AM Statement ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1949
x198
x41

asked: Dec 10 '10 at 10:57 PM

Seen: 919 times

Last Updated: Dec 10 '10 at 11:04 PM