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

Character controller sliding on edge

Discussion in 'Scripting' started by V_endeta, Dec 26, 2019.

  1. V_endeta

    V_endeta

    Joined:
    Mar 31, 2017
    Posts:
    28
    I'm using character controller for fps game but problem is when player touches any object he automatically starts sliding on edge. While player sliding on edge it's impossible to move in any direction.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(CharacterController))]
    6. public class FPSController : MonoBehaviour
    7. {
    8.     [Header("Settings")]
    9.     [Range(0f, 10f)] public float walkSpeed = 1f;
    10.     [Range(1f, 20f)] public float runSpeed = 2f;
    11.     [Range(1f, 5f)] public float jumpPower = 1f;
    12.  
    13.     [SerializeField] private float gravity = 9.81f;
    14.     [SerializeField] private CollisionFlags moveFlag;
    15.     [SerializeField] private Vector3 moveDir;
    16.  
    17.     public PhysicMaterial groundprop;
    18.     public bool isMoving = false;
    19.     public bool isRunning = false;
    20.     Vector3 lastPosition = Vector3.zero;
    21.  
    22.     [Header("Keys")]
    23.     public KeyCode runKey = KeyCode.LeftShift;
    24.     public KeyCode jumpKey = KeyCode.Space;
    25.  
    26.     CharacterController controller;
    27.     float speed;
    28.  
    29.     public GameObject gameManager;
    30.     WeaponManager weaponManager;
    31.     Shooting2 shootingScript;
    32.  
    33.     void Start()
    34.     {
    35.         controller = GetComponent<CharacterController>();
    36.         weaponManager = gameManager.GetComponent<WeaponManager>();
    37.         shootingScript = GetComponent<Shooting2>();
    38.         speed = walkSpeed;
    39.     }
    40.  
    41.     void Update()
    42.     {
    43.         float mSpeed = walkSpeed;
    44.         if(Input.GetKey(runKey)) mSpeed = RunSpeed(); else speed = walkSpeed;
    45.  
    46.         float hMove = Input.GetAxisRaw("Horizontal") * mSpeed;
    47.         float vMove = Input.GetAxisRaw("Vertical") * mSpeed;
    48.      
    49.         if(moveFlag == CollisionFlags.Below) moveDir = (hMove * transform.right) + (vMove * transform.forward);
    50.         moveDir.y -= gravity * Time.deltaTime;
    51.  
    52.         if(Input.GetKeyDown(jumpKey) && controller.isGrounded) moveDir.y = jumpPower;
    53.  
    54.         moveFlag = controller.Move(moveDir * Time.deltaTime);
    55.  
    56.         //Footsteps
    57.         isMoving = IsMoveing();
    58.         lastPosition = transform.position;
    59.  
    60.         //Weapon Heands Animations
    61.         if(isMoving && !shootingScript.isReloading && !shootingScript.isShooting && weaponManager.usingWeapon)
    62.         {
    63.             if(mSpeed == runSpeed) {
    64.                 isRunning = true;
    65.                 weaponManager.pistol.animator.SetBool("Walking", false);
    66.                 weaponManager.pistol.animator.SetBool("Running", true);
    67.             }
    68.             else {
    69.                 weaponManager.pistol.animator.SetBool("Running", false);
    70.                 weaponManager.pistol.animator.SetBool("Walking", true);
    71.                 isRunning = false;
    72.             }
    73.         }
    74.         else
    75.         {
    76.             weaponManager.pistol.animator.SetBool("Walking", false);
    77.             weaponManager.pistol.animator.SetBool("Running", false);
    78.         }
    79.     }
    80.  
    81.     public bool inAir()
    82.     {
    83.         if(controller.isGrounded) return false;
    84.         else return true;
    85.     }
    86.  
    87.     float RunSpeed()
    88.     {
    89.         speed += Time.deltaTime * 2f;
    90.         speed = Mathf.Clamp(speed, 0f, runSpeed);
    91.      
    92.         return speed;
    93.     }
    94.  
    95.     bool IsMoveing()
    96.     {
    97.         if(lastPosition == transform.position) return false;
    98.         else return true;
    99.     }
    100.  
    101.     void OnControllerColliderHit(ControllerColliderHit hit)
    102.     {
    103.         groundprop = hit.collider.material;
    104.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    I haven't combined CC with Rigidbody... there are some considerations however, but google will guide you there.

    One thing to try just off the top of my head is don't execute line 54 when your movement is very small, like only execute it when
    moveDir.magnitude > 0.01f
    or so.
     
  3. V_endeta

    V_endeta

    Joined:
    Mar 31, 2017
    Posts:
    28
    Thanks for the comment. I add

    if(moveDir.magnitude > .01f) moveFlag = controller.Move(moveDir * Time.deltaTime);

    but ofc I still have same problem.
     
  4. V_endeta

    V_endeta

    Joined:
    Mar 31, 2017
    Posts:
    28
    is there any solution to this or is this unity bug ?