Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can't move when fall from high

Discussion in '2D' started by JackReivaj, Mar 21, 2021.

  1. JackReivaj

    JackReivaj

    Joined:
    Feb 7, 2021
    Posts:
    33
    Hi!! I'm doing a platform game, my first game and when I fall from very high with my player, he gets stuck on the ground and I can't move. Is there any solution to this? Has it happened to someone already? Notice that when in my player's rigidbody I put continuous in collision detection it doesn't stuck. But the problem is that I have to leave it in discrete. Because with continuous it doesn't let me kill enemies. I don't pass the script because I don't know which one i should pass and I have several, I have the script of the player, of the enemy, of player's feet which is for kill the enemy when I touch him with feets. If it is necessary to put some code I put it but maybe the same thing happened to someone.
     
  2. rizenmusic

    rizenmusic

    Joined:
    Oct 2, 2019
    Posts:
    23
    Hi Jack) What do you mean by "continuous" doesn't let you kill your enemies? That sounds like a problem itself, you can post the code that you're using to damage enemies. I'm using continuous collision detection and it works fine.
    As for your main problem - when Rigidbody2D speed is high enough, it sometimes can't detect collisions. You need to either limit your falling speed using something like (if rigidbody velocity.y < maxFallingVelocity, then set it to maxFallingVelocity). Another way is to set your GravityScale to lower values when you fall. Same method: if rigidbody velocity.y < 0, set gravity to someValue.

    P.S. maxFallingVelocity should be less than zero.
     
  3. JackReivaj

    JackReivaj

    Joined:
    Feb 7, 2021
    Posts:
    33
    Thanks for answering. Now I'm going to review what you put me right and try it but in the meantime I'll give you the code I use to kill the enemies.

    Code (CSharp):
    1. public class FeetsLogic : MonoBehaviour
    2. {
    3.     public float rebound;
    4.     public PlayerLogic player;
    5.  
    6.     private void OnTriggerEnter2D(Collider2D c1)
    7.     {
    8.         if (c1.tag == "platform")
    9.         {
    10.             player.OnTheGround = true;
    11.             player.fall.Play();
    12.         }    
    13.         if (c1.tag == "enemy")
    14.         {
    15.             player.Jump.Play();
    16.             Destroy(c1.gameObject);
    17.             player.bodyPlayer.velocity = new Vector2(player.bodyPlayer.velocity.x, rebound);
    18.         }
    19.     }
    20.     private void OnTriggerExit2D(Collider2D c2)
    21.     {
    22.         if (c2.tag == "platform")
    23.         {
    24.             player.OnTheGround = false;
    25.         }
    26.     }
    27. }
    28.  
    Bodyplayer is the Rigidbody2d of the player.
     
  4. JackReivaj

    JackReivaj

    Joined:
    Feb 7, 2021
    Posts:
    33
    I put this code and the problem is that when it reaches the maximum speed when falling I do not know how to make it return to the speed it had before.

    Code (CSharp):
    1.  
    2. public float maxFallingVelocity= -1;
    3.  
    4. void Update()
    5. {
    6. if (bodyPlayer.velocity.y < maxFallingVelocity)
    7.         {
    8.             bodyPlayer.velocity = new Vector2 (bodyPlayer.velocity.x, maxFallingVelocity);
    9.         }
    10. }
    I also tried this code but the same thing happens to me, once I change gravity I don't know how to make it return to the original gravity.

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4. if (bodyPlayer.velocity.y < -50)
    5.         {
    6.             bodyPlayer.gravityScale = 9;
    7.         }
    8. }
     
  5. rizenmusic

    rizenmusic

    Joined:
    Oct 2, 2019
    Posts:
    23

    You can change GravityScale back when you're back on the ground, you just need another variable for your initial gravity. As for your onTriggerEnter code I don't see any problems with it. Have you tried debugging it with Debug.log? What happens on contact when your collision check is set to continious?

    Max Velocity - you don't have to change it back. When you're on the ground it becomes zero and it limites your falling speed only when falling, right?
     
  6. JackReivaj

    JackReivaj

    Joined:
    Feb 7, 2021
    Posts:
    33
    Well I tried to do a debug.log and when I collide with the enemy it reads the debug.log perfectly, I don't know why in that case it doesn't destroy it. Anyway, as it doesn't work that way, I set the collision detection to "discrete" again.
    With maximum speed it happened to me that once I reached it it did not return to 0. It kept at that speed.
    What I did was continue with the gravity code as you mentioned to me and I put it that when it hits the ground it will have the same value as it originally had. At the moment I tested it and it seems that it works. The code is like this:

    Code (CSharp):
    1. Update () {
    2. if (bodyPlayer.velocity.y <-50f)
    3.         {
    4.             bodyPlayer.gravityScale = 1;
    5.         if (OnTheFloor)
    6.             {
    7.                 bodyPlayer.gravityScale = 5;
    8.             } else if ((bodyPlayer.velocity.y> 50f))
    9.             {
    10.                 bodyPlayer.gravityScale = 1;
    11.                 if (OnTheFloor)
    12.                 {
    13.                     bodyPlayer.gravityScale = 5;
    14.                 }
    15.                
    16.             }
    17.         }
    18. }
    I had to add below that in the update:
    Code (CSharp):
    1. if (OnTheFloor)
    2.         {
    3.             bodyPlayer.gravityScale = 5;
    4.         }
    Since otherwise it happened that when it collided with a platform that moved vertically the gravityScale didn't know why it was set to 1 and the original gravity was 5. Thanks for all the help!