Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I get my CharacterController to Slide? (then crawl when it stops)

Discussion in 'Physics' started by joshuatg777, Aug 28, 2020.

  1. joshuatg777

    joshuatg777

    Joined:
    Aug 15, 2020
    Posts:
    6
    I'm making a game where you can press "q" to slide when you're at a certain speed. If you're too slow, or when you eventually slow down from the slide, you start "crawling" (moving slow). I can make the CharacterController crawl, but I can't make it slide (it seems to just reverse your controls while crawling).
    This is my code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     public CharacterController controller;
    9.  
    10.     public float speed = 12f;
    11.     public float crawlSpeed = 0f;
    12.     public float gravity = -9.81f;
    13.     public float jumpHeight = 3f;
    14.     public float crouchHeight = 0.5f;
    15.     public float slideDuration = 1f;
    16.  
    17.     public Transform groundCheck;
    18.     public float groundDistance = 0.4f;
    19.     public LayerMask groundMask;
    20.  
    21.     private float slideMinus;
    22.     private float pSpeed;
    23.     private float speedBackup;
    24.     private Vector3 scaleBackup;
    25.     private Vector3 move2;
    26.  
    27.     Vector3 velocity;
    28.     bool isGrounded;
    29.  
    30.     private void Start()
    31.     {
    32.         print(transform.right);
    33.         print(transform.forward);
    34.         pSpeed = speed;
    35.         speedBackup = speed;
    36.         scaleBackup = transform.localScale;
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.         if(Input.GetKey("q") == false)
    43.         {
    44.             transform.localScale = scaleBackup;
    45.         }
    46.  
    47.         pSpeed = speedBackup;
    48.  
    49.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    50.  
    51.         if(isGrounded && velocity.y < 0)
    52.         {
    53.             velocity.y = -5f;
    54.         }
    55.  
    56.         float x = Input.GetAxis("Horizontal");
    57.         float z = Input.GetAxis("Vertical");
    58.  
    59.         Vector3 move = transform.right * x + transform.forward * z;
    60.         move2 = move;
    61.  
    62.         if(Input.GetKey("q"))
    63.         {
    64.             StartCoroutine("Slide");
    65.  
    66.             pSpeed = crawlSpeed;
    67.  
    68.             Crouch();
    69.         }
    70.  
    71.      
    72.         controller.Move(move * pSpeed * Time.deltaTime);
    73.      
    74.  
    75.         if (Input.GetButtonDown("Jump") && isGrounded)
    76.         {
    77.             velocity.y = Mathf.Sqrt(jumpHeight * -5f * gravity);
    78.         }
    79.  
    80.         velocity.y += gravity * Time.deltaTime;
    81.  
    82.         controller.Move(velocity * Time.deltaTime);
    83.     }
    84.  
    85.     void Crouch()
    86.     {
    87.         transform.localScale = new Vector3(transform.localScale.x, crouchHeight, transform.localScale.z);
    88.     }
    89.  
    90.     IEnumerator Slide()
    91.     {
    92.         Vector3 moveLocked = move2 * pSpeed * Time.deltaTime;
    93.         controller.Move(new Vector3(moveLocked.x * -0.5f, moveLocked.y, moveLocked.z * -0.5f));
    94.         yield return new WaitForSeconds(1);
    95.     }
    96. }
    Any idea how to fix this?

    NOTE: I'm a beginner to Unity.
     
  2. SlimPug_Gamer

    SlimPug_Gamer

    Joined:
    Aug 8, 2020
    Posts:
    1
    i not know