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

Question How to fix character falling down stairs when not moving

Discussion in 'Scripting' started by Markasabrt, Feb 16, 2023.

  1. Markasabrt

    Markasabrt

    Joined:
    Oct 2, 2021
    Posts:
    3
    I have made a character movement script and made stairs, i set the stairs to convex and walking up them is just fine but when i stand still on them my character falls down. Im using a rigidbody for the character. Is there any way to fix this? I can show my code if that helps
     
    Last edited: Feb 16, 2023
  2. ManuelRauber

    ManuelRauber

    Joined:
    Apr 3, 2015
    Posts:
    114
    Code and Video of the issue is always helpful.
    Also Collider Setup, Physics Materials
     
  3. Markasabrt

    Markasabrt

    Joined:
    Oct 2, 2021
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     [Header("Movement")]
    9.     public float moveSpeed;
    10.     public float gravity;
    11.     public float groundDrag;
    12.  
    13.     public float jumpForce;
    14.     public float jumpCooldown;
    15.     public float airMultiplier;
    16.     public float gravityHeight;
    17.  
    18.     bool readyToJump;
    19.    
    20.  
    21.     [HideInInspector] public float walkSpeed;
    22.     [HideInInspector] public float sprintSpeed;
    23.  
    24.     [Header("Keybinds")]
    25.     public KeyCode jumpKey = KeyCode.Space;
    26.  
    27.     [Header("Ground Check")]
    28.     public float playerHeight;
    29.     public LayerMask whatIsGround;
    30.     bool grounded;
    31.    
    32.     public Transform orientation;
    33.  
    34.     float horizontalInput;
    35.     float verticalInput;
    36.  
    37.     Vector3 moveDirection;
    38.  
    39.     Rigidbody rb;
    40.  
    41.  
    42.     private void Start()
    43.     {
    44.         rb = GetComponent<Rigidbody>();
    45.         rb.freezeRotation = true;
    46.  
    47.         readyToJump = true;
    48.     }
    49.  
    50.     private void Update()
    51.     {
    52.         if (!Physics.Raycast(transform.position, -transform.up, gravityHeight))
    53.         {
    54.             gravity = 18;
    55.         }
    56.  
    57.         // ground check
    58.             grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround);
    59.  
    60.         MyInput();
    61.         SpeedControl();
    62.  
    63.         // handle drag
    64.         if (grounded)
    65.             rb.drag = groundDrag;
    66.         else
    67.             rb.drag = 0;
    68.  
    69.         if (Input.GetKey("left shift"))
    70.         {
    71.             moveSpeed = 19f;
    72.         }
    73.         else
    74.         {
    75.             moveSpeed = 15f;
    76.         }
    77.     }
    78.  
    79.     private void FixedUpdate()
    80.     {
    81.         MovePlayer();
    82.         GetComponent<Rigidbody>().AddForce(Vector3.down * gravity * GetComponent<Rigidbody>().mass);
    83.     }
    84.  
    85.     private void MyInput()
    86.     {
    87.         horizontalInput = Input.GetAxisRaw("Horizontal");
    88.         verticalInput = Input.GetAxisRaw("Vertical");
    89.  
    90.         // when to jump
    91.         if (Input.GetKey(jumpKey) && readyToJump && grounded)
    92.         {
    93.             readyToJump = false;
    94.  
    95.             Jump();
    96.  
    97.             Invoke(nameof(ResetJump), jumpCooldown);
    98.         }
    99.     }
    100.  
    101.     private void MovePlayer()
    102.     {
    103.         // calculate movement direction
    104.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    105.  
    106.         // on ground
    107.         if (grounded)
    108.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    109.  
    110.         // in air
    111.         else if (!grounded)
    112.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    113.     }
    114.  
    115.     private void SpeedControl()
    116.     {
    117.         Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    118.  
    119.         // limit velocity if needed
    120.         if (flatVel.magnitude > moveSpeed)
    121.         {
    122.             Vector3 limitedVel = flatVel.normalized * moveSpeed;
    123.             rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
    124.         }
    125.     }
    126.  
    127.     private void Jump()
    128.     {
    129.         // reset y velocity
    130.         rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    131.  
    132.         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    133.     }
    134.     private void ResetJump()
    135.     {
    136.         readyToJump = true;
    137.     }
    138. }
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    This doesn't tell us anything about the collider itself.
    Are there steps? Is it sloped? etc.
     
  5. Markasabrt

    Markasabrt

    Joined:
    Oct 2, 2021
    Posts:
    3