Setting initial movement for a RigidBody2D

Why can’t I use AddForce in the Start() event/message?

I have tried this:

Rigidbody2D rb;

void Start () {
    rb = GetComponent<Rigidbody2D>();

    rb.MovePosition(new Vector2 (-5.0f, -5.0f));  // works. Just for testing

    rb.AddForce(new Vector2 (30.0f, 50.0f));      // no movement produced
}

The MovePosition() function works (used just for testing purposes), but not the AddForce() call. Why?

More strangely, if I set the velocity direcly (not a good practice according to the manuals) it works fine!:

void Start () {
	rb = GetComponent<Rigidbody2D>();
    // Works:
	rb.velocity = new Vector2 (3.0f, 5.0f);
	// Doesn't work:	
	rb.AddForce(new Vector2(30.0f, 50.0f));

I have reached this solution in order to use AddForce to set the initial movement, but I find it inefficient and not very elegant:

void Start () {
    pushed = false;
    rb = GetComponent<Rigidbody2D>();
}

void Update () {
    if (!pushed) {
	    rb.AddForce(new Vector2(30.0f, 50.0f));
		pushed = true;
	}

}

Is there a better solution?

Thanks in advance!

Hello again!

Well, this is crazy! Please don’t think I’m kidding, this is really what I get (I’m seriously considering to record the issue as a proof that I’m not kidding).

The first thing I’ve done is what you suggested, MelvMay: create a new project. 2D and no gravity, empty object with rigidbody2d. Everything went perfect with your line of code.

Then I went back to my project, and inserted the same line of code… with the result that this time the object moved! But trying to isolate the problem I reproduced it again.

I went back to the project with your line of code and tried different cases, and finally reproduced this strange behaviour. I added a movement before the force, like this:

GetComponent<Rigidbody2D>().MovePosition(new Vector2(-1.0f, -1.0f));
GetComponent<Rigidbody2D>().AddForce(new Vector2(-10.0f, -15.0f));

with the result that the object is repositioned to (-1,-1) but remains still at that position. The call to AddForce is completely ignored!! I removed the first line inserting a // comment prefix, and the object moved again (starting at (0,0), of course).

If I swap the lines same result (repositioning ok, no movement).

Could you please try this variation and tell me what you get? (just in case please use negative vectors, as in my project I found cases where the object moved but to the RIGHT, even when applied force had negative value in the X axis)

The problem seems to be produced by the combination of movement and force application… this is completely nonsense, so I’m starting to think this could be caused by I corrupt installation (too serious to be a undetected bug). I’m running Unity 5.0.0 under a Windows XP on an oldish laptop, and in the Roll-a-ball tutorial I found that I can’t see the shadows. In the forum other users commented the same issue, which seems related with old DirectX not rendering the shadows. I realize this force problem is very different and shouldn’t be related with DirectX, but maybe Unity 5 and oldish systems don’t mix together very well.

Thanks for your patience and your explanation on the Box2D!