- Home /
AddForce not working on Rigidbody2D.
I have a floating island that Translates up and down with a Rigidbody2D (Gravity Scale = 0). I'm trying to AddForce it upwards when the Player lands on it but it just floats down with the character. I've tried disabling the 'floating' but AddForce still won't kick in. Debug.Log shows Player entering the collider.
bool goUp = false;
void Update ()
{
if (goUp == true)
driftUp ();
else if (goUp == false)
driftDown ();
}
void driftUp ()
{
transform.Translate (Vector2.up * .05f * Time.deltaTime);
if (transform.position.y > -.9f)
goUp = false;
}
void driftDown ()
{
transform.Translate (-Vector2.up * .05f * Time.deltaTime);
if (transform.position.y < -2)
goUp = true;
}
void OnCollisionEnter2D (Collision2D contact)
{
if (contact.gameObject.tag == "Player")
rigidbody2D.AddForce (Vector2.up * 100f * Time.deltaTime);
}
have you tryed to add Gravity Scale ? And to addForce up at the island to keep it flying?
Yea, I returned Gravity Scale to 1 (Physics2D, -30), Disabled "driftUp/Down", and added a simple AddForce with Vector.Up to keep it in place. Player jumped on it, and still no extra force.
Try to add new empty game object attached to player, positioned under his position (not much). Floating islands need to have layer mask, ie. called "islands" or what you want. Then check if player stands on "island" with this code:
`Physics2D.Linecast(transform.position, groundCheck.transform.position, 1 << Layer$$anonymous$$ask.NameToLayer("islands"));`
where groundCheck is your new empty object. Then just add a condition if player stands on floating island and add force. I had the same problem when I had checked Apply root motion. Gravity scale and mass set to 1.
Strangely enough, it was the Time.deltaTime on line 30 breaking things. I removed it and now my Player bounces on the island as though it were a trampoline.