Search Unity

Character Controller 'Dash' acting weird.

Discussion in 'Scripting' started by QuincyOnIce, Dec 31, 2019.

  1. QuincyOnIce

    QuincyOnIce

    Joined:
    Jul 11, 2019
    Posts:
    1
    Hello, I'm working on a pretty simple dash function for my character controller. But there's a pretty weird problem.







    As you can see in the first picture, the 'silhouettes' move in a straight line backwards, which is what I want. However, in the second picture, they move diagonally. Which isn't what I want. This is strange to me since the one Forwards uses transform.forward, while the one backwards uses -transform.forward. Therefore I have no Idea why It doesn't work.

    Code (CSharp):
    1. private void Dash(Vector3 Direction)
    2.     {
    3.         GameObject WarpClone = Instantiate(gameObject);
    4.         WarpClone.transform.position = transform.position;
    5.         Destroy(WarpClone.GetComponent<CharacterMovement>());
    6.         Destroy(WarpClone.GetComponent<CharacterController>());
    7.         Destroy(WarpClone.GetComponent<Animator>());
    8.         foreach (Transform child in WarpClone.GetComponentsInChildren<Transform>())
    9.         {
    10.             Destroy(child.GetComponent<Collider>());
    11.             SkinnedMeshRenderer[] SMRs = WarpClone.GetComponentsInChildren<SkinnedMeshRenderer>();
    12.             MeshRenderer[] MRs = WarpClone.GetComponentsInChildren<MeshRenderer>();
    13.             foreach (SkinnedMeshRenderer SMR in SMRs)
    14.             {
    15.                 SMR.material = warpMaterial;
    16.             }
    17.             foreach (MeshRenderer MR in MRs)
    18.             {
    19.                 MR.material = warpMaterial;
    20.             }
    21.         }
    22.         Destroy(WarpClone, 100f);
    23.         CC.Move(Direction * dashStrength);
    24.     }
    Here's the code for actually moving and creating the 'silhouettes'.

    Code (CSharp):
    1. private void DashFuncW()
    2.     {
    3.         if (Time.realtimeSinceStartup - timeOfFirstButton < DoublePressSpan && Input.GetKeyDown(KeyCode.W) && firstButtonPressed)
    4.         {
    5.             Dash(transform.forward);
    6.  
    7.             firstButtonPressed = false;
    8.             timeOfFirstButton = 0f;
    9.         }
    10.         if (Input.GetKeyDown(KeyCode.W))
    11.        {
    12.             timeOfFirstButton = Time.realtimeSinceStartup;
    13.             firstButtonPressed = true;
    14.        }
    15.     }
    16.     private void DashFuncS()
    17.     {
    18.         if (Time.realtimeSinceStartup - timeOfFirstButton < DoublePressSpan && Input.GetKeyDown(KeyCode.S) && firstButtonPressed)
    19.         {
    20.             Dash(-transform.forward);
    21.  
    22.             firstButtonPressed = false;
    23.             timeOfFirstButton = 0f;
    24.         }
    25.         if (Input.GetKeyDown(KeyCode.S))
    26.         {
    27.             timeOfFirstButton = Time.realtimeSinceStartup;
    28.             firstButtonPressed = true;
    29.         }
    30.     }
    And here's the code to get the double tap input.

    I'd appreciate any issues you can see.
    Personally, I suspect it has something to do with the Character Controller colliding somehow with the ground, and therefore sliding diagonally.
     
    Last edited: Dec 31, 2019
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181