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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question My player does not fall when going into the side of a platform

Discussion in '2D' started by El_barto8, Jan 16, 2022.

  1. El_barto8

    El_barto8

    Joined:
    Oct 22, 2020
    Posts:
    8
    Hi, im trying to create a 2d platformer game. Heres the code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class playerMovement : MonoBehaviour
    4. {
    5.     public float playerSpeed = 5.0f;
    6.     private static float jumpPower = 5.0f;
    7.     public LayerMask layerMask;
    8.     public Transform groundCheck;
    9.  
    10.     public Rigidbody2D rb2d;
    11.  
    12.     private void Start() => rb2d = GetComponent<Rigidbody2D>();
    13.  
    14.     private void Update()
    15.     {
    16.         float xInput = Input.GetAxisRaw("Horizontal");
    17.         rb2d.velocity = new Vector2(xInput * playerSpeed, rb2d.velocity.y);
    18.  
    19.         switch (xInput)
    20.         {
    21.             case 1:
    22.                 transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
    23.                 break;
    24.             case -1:
    25.                 transform.localRotation = Quaternion.Euler(0f, -180f, 0f);
    26.                 break;
    27.         }
    28.  
    29.         var isGrounded = Physics2D.Raycast(groundCheck.position, Vector2.down, 0.1f, layerMask);
    30.         if (Input.GetButtonDown("Jump") && isGrounded)
    31.         {
    32.             rb2d.velocity = new Vector2(rb2d.velocity.x, jumpPower);
    33.         }
    34.  
    35.         if (Input.GetKeyDown(KeyCode.R))
    36.         {
    37.             transform.position = new Vector3(-7f, -2f);
    38.         }
    39.     }
    40. }
    The problem here is as when i fall and walk into the side of a platform the player does not fall and stays there until x axis is released. Can anyone help me? (btw sorry for my bad english)
     

    Attached Files:

  2. Cletus2000

    Cletus2000

    Joined:
    May 20, 2018
    Posts:
    3
    I faced the same problem some months ago and ended implementing movement in 3 possible ways as i can remember:
    -Changing the velocity of the rigidbody (as you did)
    -Adding force to the rigidbody
    -Simply changing the position in the transform

    I do NOT recommend you the second option, because you are constantly adding acceleration, wich becomes so hard to control.
    As an implementation of the third option, i wrote this code:
    Code (CSharp):
    1. Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);  //Getting the input as an horizontal vector instead of a float value
    2. transform.position += movement* Time.deltaTime * speed;  //Add that new vector to the current position
    You can try this or maybe creating a physic material with no friction, so instead of getting glued to the wall, it slips down, but this option is much less handy and i dont know if it could give you any problems in the future.
     
    El_barto8 likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Either physics is in control of the Transform or you are. A Rigidbody2D cannot do its work if you're taking charge of the Transform. You need to spend some time understanding what things must obviously be doing and less so on logic and code.

    By this I mean, step back and think about it: How does a Rigidbody2D get all the physics stuff back into something you can see? How does it "move"? By writing its position/rotation to the Transform. So what are you doing? Writing to the Transform. So when two people start typing on the same keyboard, it's going to be a mess.

    Then there's the question of, why do things fall in the first place? Gravity! What could I do to stop gravity working? Well you could stomp over its effects which are (by default) modifying the Y component of the body velocity (up/down). You stomp over that, Gravity is gone. What about stomping over the X component of the velocity? Won't see much gravity stomping there unless of course you're setting the velocity so that most of the force is the solver trying to get you out of a wall because you're "pushing" it hard against it. It's trying to desperately solve that part to stop overlaps.

    Then we have the usual, change everything per-frame without understanding that physics doesn't run per-frame, it runs (by default) during the FixedUpdate which (by default) is 50Hz (FPS). You can run physics per-frame if you wish but it starts with understanding the concept here.

    The problem with "workarounds" for problems are that the devs are creating the problem themselves. It's not a physics problem, it's understanding why you're fighting the physics in the first place. :)

    The single most important rule: When using 2D physics for movement, move a Rigidbody2D and use its API to effect the move. If you do anything else, you're doing it incorrectly. The Rigidbody2D is your proxy to the Transform.
     
    El_barto8 likes this.
  4. El_barto8

    El_barto8

    Joined:
    Oct 22, 2020
    Posts:
    8
    Thank you for helping. I can finally understand that now. I knew that changing the velocity wasnt the best option but i couldnt find a better option. Really thanks
     
  5. El_barto8

    El_barto8

    Joined:
    Oct 22, 2020
    Posts:
    8
    Thank you for replying and helping me. This solution works but if i go into the side of a wall it gets kinda buggy and the player shakes while falling down. Anyways thanks for the help mate
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    It'll go into the side of the wall because you're modifying the Transform and bypassing physics as i mentioned previously. You cause overlap then when physics runs, the solver has to sort it out.

    Don't modify the Transform. :)
     
    El_barto8 and Kurt-Dekker like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    This also counts for modifying the transform. Don't do it when you're using physics. You will move the collider and it will trigger anger and discontent in the physics system.

    Instead, if you MEAN to move the collider, use .MoveRotation() , or ideally just move a child object that contains the visual portions of what you want, and does NOT contain the colliders.
     
    El_barto8 likes this.
  8. El_barto8

    El_barto8

    Joined:
    Oct 22, 2020
    Posts:
    8
    Thank you. I know this will sound dumb, but what could be the solution for this problem?
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Let me repeat MelvMay:

    That's it. That's the solution.

    At a minimum you are violating this fundamental tenet on lines 22, 25 and 37. Fix that problem first (see above!) and then see where you stand.

    I'll add my own standard blurb:

    With Physics (or Physics2D), never manipulate the transform directly: if you do that, you are bypassing the physics system.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) to move things.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    With a wheel collider, use the motor or brake torque to effect motion.