How do you use Rigidbody2D.AddForce

Because this feature is new I can find any guides online on how to use it.

The scripting reference has:

AddForce(force: Vector2): void;

What parts of this am I meant to change?

just using this code in my script

function FixedUpdate () 
{
	if(Input.GetKey ("d")) 
	{
		AddForce(force: Vector2): void;
	}
}

gives the errors:

“Assets/My script/Move.js(6,26): BCE0043: Unexpected token: force.”

“Assets/My script/Move.js(6,41): BCE0043: Unexpected token: :.”

“Assets/My script/Move.js(6,42): UCE0001: ‘;’ expected. Insert a semicolon at the end.”

How do I use this?

1 Answer

1

AddForce(force: Vector2): void;

is a method signature, not example of calling it. It means that this is a method accepting one parameter of type Vector2 and returning no value (void). Sample usage:

rigidbody2D.AddForce(Vector2.up);

Thanks, why don't they have example code in the scripting reference?

–

I just tested this and now I get "Assets/My script/Move.js(6,17): BCE0005: Unknown identifier: 'AddForce'."

–

I guess they either missed it, trying to update documentation pages for new 4.3 classes, or maybe they just decided that sample is not required in this case. There are more pages in official docs, that lack examples.

–

You have to call this method on actual Rigidbody2D instance. Answer updated.

–

new code gives me "Assets/My script/Move.js(7,17): BCE0004: Ambiguous reference 'rigidbody2D': Move.rigidbody2D, UnityEngine.Component.rigidbody2D."

–