Search Unity

Resolved Player obj getting stuck on walls

Discussion in 'Physics' started by Nagonn, Dec 6, 2023.

  1. Nagonn

    Nagonn

    Joined:
    Sep 21, 2023
    Posts:
    11
    i know that this is not a new issue, but everything ive tried has not worked, so I beg you smart people for help.

    not all fo the code is important
    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.     [Header("Movement")]
    4.  
    5.     public GameObject player;
    6.     public bool isWalking;
    7.     public AudioSource walkingSFX;
    8.     public bool audioAlreadyPlayed = false;
    9.  
    10.  
    11.     public float moveSpeed;
    12.  
    13.     public float groundDrag;
    14.  
    15.     public float jumpForce;
    16.     public float jumpCooldown;
    17.     public float airMultiplier;
    18.     private bool readyToJump;
    19.  
    20.     [Header("Keybinds")]
    21.     public KeyCode jumpKey = KeyCode.Space;
    22.  
    23.  
    24.     [Header("Ground Check")]
    25.     public float playerHeight;
    26.     public LayerMask whatIsGround;
    27.     public bool grounded;
    28.  
    29.     public Transform orientation;
    30.  
    31.     float horizontalInput;
    32.     float verticalInput;
    33.  
    34.     Vector3 moveDirection;
    35.     Rigidbody rb;
    36.  
    37.     private void Start()
    38.     {
    39.         rb = GetComponent<Rigidbody>();
    40.         rb.freezeRotation = true;
    41.         readyToJump = true;
    42.     }
    43.  
    44.     // every frame
    45.     private void Update()
    46.     {
    47.         MovingSFX();
    48.  
    49.         grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
    50.  
    51.         MyInput();
    52.         SpeedControl();
    53.  
    54.  
    55.  
    56.         if (grounded == true)
    57.         {
    58.             rb.drag = groundDrag;
    59.  
    60.         }
    61.         else
    62.         {
    63.             rb.drag = 0;
    64.  
    65.         }
    66.  
    67.     }
    68.  
    69.  
    70.  
    71.     // set fps
    72.     private void FixedUpdate()
    73.     {
    74.         MovePlayer();
    75.  
    76.     }
    77.  
    78.  
    79.     // Keybinds to move I think:
    80.  
    81.     // when can jump and axis inputs
    82.     private void MyInput()
    83.     {
    84.         horizontalInput = Input.GetAxisRaw("Horizontal");
    85.         verticalInput = Input.GetAxisRaw("Vertical");
    86.  
    87.         if (Input.GetKey(jumpKey) && readyToJump == true && grounded == true)
    88.         {
    89.             readyToJump = false;          
    90.  
    91.             Jump();
    92.  
    93.             Invoke(nameof(ResetJump), jumpCooldown);
    94.         }            
    95.     }
    96.  
    97.  
    98.     // WALKIN N SHID:
    99.  
    100.     private void MovePlayer()
    101.     {
    102.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    103.  
    104.         rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    105.  
    106.         if (grounded)
    107.         {
    108.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    109.         }
    110.  
    111.         else if (!grounded)
    112.         {
    113.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    114.         }
    115.     }
    116.  
    117.     // stops you from zoomin n sliding all over
    118.     private void SpeedControl()
    119.     {
    120.         Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    121.  
    122.         if (flatVel.magnitude > moveSpeed)
    123.         {
    124.             Vector3 limitedVel = flatVel.normalized * moveSpeed;
    125.             rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
    126.         }
    127.     }
    128.  
    129.  
    130.     // how to jump
    131.  
    132.     private void Jump()
    133.     {
    134.         rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    135.  
    136.         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    137.     }
    138.  
    139.     private void ResetJump()
    140.     {
    141.         readyToJump = true;
    142.     }
    143.  
    144.     void MovingSFX()
    145.     {
    146.         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
    147.         {
    148.             isWalking = true;
    149.         }
    150.         else
    151.         {
    152.             isWalking = false;
    153.         }
    154.  
    155.         if (isWalking == true && grounded == true && audioAlreadyPlayed == false)
    156.         {
    157.             walkingSFX.Play();
    158.             audioAlreadyPlayed = true;
    159.         }
    160.  
    161.         if (grounded == false && audioAlreadyPlayed == true)
    162.         {
    163.             walkingSFX.Stop();
    164.             audioAlreadyPlayed = false;
    165.         }
    166.  
    167.         if (isWalking == false && audioAlreadyPlayed == true)
    168.         {
    169.             audioAlreadyPlayed = false;
    170.             walkingSFX.Stop();
    171.         }
    172.        
    173.        
    174.     }
    175. }
    176.  
    upload_2023-12-5_20-37-8.png
    upload_2023-12-5_20-37-17.png
     

    Attached Files:

  2. Nagonn

    Nagonn

    Joined:
    Sep 21, 2023
    Posts:
    11
    dont think I showed, but the physics material has dynamic and static friction set to 0
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,000
    In your player's physics material properties set the 'Friction Combine' setting to Minimum.

    Explanation:

    When a collision occurs the physics engine combines the friction value from the physics materials of all the objects that were involved in the collision. If a wall doesn't have any physics material set then it will use the default physics material. So your zero friction player is hitting a wall that has some friction and is getting stuck.
     
    Last edited: Dec 6, 2023
  4. Nagonn

    Nagonn

    Joined:
    Sep 21, 2023
    Posts:
    11
    You are a beautiful person and I love you