- Home /
Unity5 rigidbody2d not working properly... :(
I have a playercontroller script that handles the gravity and jumping of the player.. but what happens is the player is bearly jumping and falling. But my script is fine with unity4, after i upgrade my game on unity5 the problem occurs... please help.. There is a function applygravity there and jump function you can ctrl+f to search, sorry for the mess :)
note: no other script is handling my physics for the player except this playercontroller script
by the way my game is like flappy bird style...
===================================================================================================
/// <summary>
/// the logic of the gameloop of this class
/// </summary>
/// <returns></returns>
IEnumerator OnUpdate()
{
//start catching if the user taps the screen
StartCoroutine("CheckTap");
//do this only if the player is not dead
while (true)
{
switch (playerState)
{
case PlayerState.Idle:
break;
case PlayerState.Flying:
//when the player is not on the ground
if (!groundCheck.IsGrounded || GameController.Instance.debugMode)
{
//do the calcualtions on the air
DoAirLogic();
//dont let the player go up offscreen
RestrictVerticalPosition();
}
break;
case PlayerState.Dead:
//stop checking if the player is dead
StopCoroutine("CheckTap");
playerAnimator.PauseAnimation();
DidTap = false;
if (groundCheck.IsGrounded && !isDead && rigid2D.velocity == Vector2.zero)
{
isDead = true;
//playerState = PlayerState.Idle;
StartCoroutine("DeathState");
}
break;
default:
break;
}
//apply gravity
ApplyGravity();
//wait for the fixed update
yield return new WaitForFixedUpdate();
}
}
#region player air and ground logic calculation
/// <summary>
/// this is where the calulations or logics when the player is not on the ground
/// </summary>
void DoAirLogic()
{
//when the user taps the screen
if (DidTap)
{
DidTap = false;
//cast a ray from the touch points on the screen to test if we tap on a powerups
RaycastHit2D touchHit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
//if the user tap on a powerup
if (touchHit.collider != null)
{
if (touchHit.collider.gameObject.tag == "powerup")
{
//get the powerups from the container and use it
PowerUpContainer pu = touchHit.collider.gameObject.GetComponent<PowerUpContainer>();
pu.ApplyMyPower();
}
}
//makes the player jump
Jump();
}
//rotate the player up wars if the player is going up or downwards if the player is giong down but only if he is grounded
//if (!groundCheck.IsGrounded)
// DoRotation();
}
/// <summary>
/// Apply the gravity only if it is enabled
/// </summary>
void ApplyGravity()
{
//if we can use gravity
if (EnableGravity)
{
//apply gravity only when we are not on the ground
rigid2D.AddForce(new Vector2(0.0f, -gravity));
}
}
/// <summary>
/// This makes player jump
/// </summary>
void Jump()
{
//play the jump sound
GetComponent<JumpSound>().PlayOnce();
//make a jumpfore vector based on the jumprate set on the inspector
Vector2 jumpFoce = new Vector2(rigid2D.velocity.x, jumpRate);
//reset the current velocity of the player
//we do not want to add up this jumpforce velocity to the current velocity of the player
rigid2D.velocity = Vector2.zero;
//apply the velocity to our rigidbody
//this makes the player to jump
rigid2D.AddForce(jumpFoce,ForceMode2D.Force);
}
#endregion
Too much code. Break it down to what u think is the part causing problems. Answers is not about code debugging you know
Same problem, I convert my Unity4 Project to Unity5 and i noticed my code
hit.GetComponent<Rigidbody2D>().gravityScale
And
hit.GetComponent<Rigidbody2D>().AddForce
Do not work correctly in Unity5...
yes, i've been searching it in days but i havent find any solotion...
Answer by GiyomuGames · Mar 05, 2015 at 11:02 AM
Do you have an animation on the player? If go to its animator and uncheck "apply root motion". It corrected the similar problem I had.
Thanks! I was freaking out because I didn't back up my game before the conversion and this was the only problem :)
yes thank you, this is one of the possible solutions
Link: Solution
Your answer
![](https://koobas.hobune.stream/wayback/20220613185102im_/https://answers.unity.com/themes/thub/images/avi.jpg)