Search Unity

Implementing A Dash With My CharacterController

Discussion in 'Scripting' started by SwaggyMcChicken, Sep 2, 2015.

  1. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    I'm developing a 3D fighter game with my friends currently and currently we have a movement system where the player will constantly face the enemy. With the transform.TransformPosition() function they essentially orbit around the enemy, when the A key is pressed it will move to the left, however since the transform.TransformPosition() and constant rotation towards the enemy, it will move in a circle. This is exactly what we wanted. But the question is, how would we implement a dash key with the Q and E keys? I attempted to have a system work similar to the basic controls, having a Vector3 just for movement regarding the dash keys and translating it with TransformPosition(), but it didn't work out very well.

    Here's my movement script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CharController : MonoBehaviour {
    5.  
    6.     //Created: August 21-28, 2015
    7.     //Creator: SwaggyMcChicken
    8.     //Purpose: Enables player to move, "orbitting" the enemy
    9.    
    10.     //Edited: September 2, 2015
    11.     //Editor: SwaggyMcChicken
    12.     //Purpose: Implementing a "Dash" feature
    13.  
    14.     public CharacterController CC;
    15.    
    16.     //Reguarding Movement section
    17.     public float JumpHeight = 0f;
    18.     public float Speed = 6f;
    19.     public float Gravity = 5f;
    20.     public float DashSpeed = 10f;
    21.     bool Dashing = false;
    22.     Vector3 MoveDirection;
    23.     Vector3 DashDirection;
    24.  
    25.  
    26.     void Update () {
    27.  
    28.         if(CC.isGrounded == true){
    29.  
    30.             //Gets player input based on Axis (WASD)
    31.             MoveDirection = new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis("Jump"), Input.GetAxis ("Vertical"));
    32.                
    33.             //Translating from local cords
    34.             MoveDirection = transform.TransformDirection (MoveDirection);
    35.                
    36.             //Multiplying to add Speed variable
    37.             MoveDirection *= Speed;
    38.  
    39.             //If let dash key is pressed & player isn't currently dashing, change Vector3
    40.             if(Input.GetKey(KeyCode.E) && Dashing == false){
    41.                
    42.                 DashDirection.x = DashDirection.x + DashSpeed;
    43.                 Dashing = true;
    44.                    
    45.             }
    46.  
    47.             //Same as above, but to the right.
    48.             if(Input.GetKey(KeyCode.Q) && Dashing == false){
    49.  
    50.                 DashDirection.x = DashDirection.x - DashSpeed;
    51.                 Dashing = true;
    52.  
    53.             }
    54.  
    55.             //Translating from local cords
    56.             DashDirection = transform.TransformDirection(DashDirection);
    57.            
    58.         }      
    59.        
    60.     }
    61.    
    62.     void FixedUpdate(){
    63.        
    64.        
    65.         if (CC.isGrounded == false) {
    66.            
    67.             //Simulating Gravity and syncs with current deltaTime
    68.             MoveDirection.y -= Gravity * Time.deltaTime;
    69.            
    70.             if (MoveDirection.y > 50) {
    71.                
    72.                 MoveDirection.y = 50;
    73.                 Debug.Log (MoveDirection.y);
    74.                
    75.             }
    76.  
    77.         }
    78.  
    79.         //Moves character
    80.         CC.Move(MoveDirection * Time.deltaTime);
    81.  
    82.         //If player is dashing, use Vector3 to dash and set Dashing to false
    83.         if (Dashing == true) {
    84.  
    85.             CC.Move (DashDirection * Time.deltaTime);
    86.             Dashing = false;
    87.  
    88.         }
    89.    
    90.     }
    91.  
    92. }
    I could be overlooking this entirely. I do it often lol. Any help would be appreciated
     
  2. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    I've been experimenting with multiple solutions, nothing has worked.
     
  3. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I usually have a float for startSpeed, one for speed. In start I set speed=startSpeed. Whenever the player dash's I set speed to x*speed. When the let go of the dash button I set speed=startSpeed again.
    Don't know if that helps you in your case
     
  4. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    Where do you start with this? I'm curious where you did that.
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I don't have access to my old scripts (on mobile atm) but from memory I had something like this in the player movement script. declare the 2 floats with a value applied to startSpeed.
    in Void Start() have speed =startSpeed;

    In movement have something like:
    If(input <'whatever you are using for movement'>){
    your normal movement stuff here (e.g moveDirection *=speed;)
    }

    If (input <'movement input & dash input>){
    speed *= whatever your dash multiplier is (or just add extra speed, or however you want)
    }
    Else speed = startSpeed;
     
  6. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    When I said dash I should have been a little more precise lol, I meant a dash, roll, or something like that, lol. This seems to be a sprinting function
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Ah, ok. Sorry my mistake (i did wonder why it seemed so complicated). I've never used rolls but will keep my eye on this so I can find out how for when I do.
     
  8. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    Lol I'll let you know if I figure it out
     
  9. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    Still looking for any tips and suggestions, been searching for some.
     
  10. BraydenB

    BraydenB

    Joined:
    Apr 5, 2020
    Posts:
    1
    Here's my not so clean code but it works. Hope this helps!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float speed = 10f;
    8.     public float dashLength = 0.15f;
    9.     public float dashSpeed = 100f;
    10.     public float dashResetTime = 1f;
    11.  
    12.     public CharacterController characterController;
    13.  
    14.     private Vector3 dashMove;
    15.     private float dashing = 0f;
    16.     private float dashingTime = 0f;
    17.     private bool canDash = true;
    18.     private bool dashingNow = false;
    19.     private bool dashReset = true;
    20.  
    21.     void Update()
    22.     {
    23.         float moveX = Input.GetAxis(“Horizontal”);
    24.         float moveZ = Input.GetAxis(“Vertical”);
    25.  
    26.         Vector3 move = transform.right * moveX + transform.forward * moveZ;
    27.  
    28.         if (move.magnitude > 1)
    29.         {
    30.             move = move.normalized;
    31.         }
    32.  
    33.  
    34.         if (Input.GetButtonDown("Dash") == true && dashing < dashLength && dashingTime < dashResetTime && dashReset == true && canDash == true)
    35.         {
    36.             dashMove = move;
    37.             canDash = false;
    38.             dashReset = false;
    39.             dashingNow = true;
    40.         }
    41.  
    42.         if (dashingNow == true && dashing < dashLength)
    43.         {
    44.             characterController.Move(dashMove * dashSpeed * Time.deltaTime);
    45.             dashing += Time.deltaTime;
    46.         }
    47.  
    48.         if (dashing >= dashLength)
    49.         {
    50.             dashingNow = false;
    51.         }
    52.  
    53.         if (dashingNow == false)
    54.         {
    55.             characterController.Move(move * speed * Time.deltaTime);
    56.         }
    57.  
    58.         if (dashReset == false)
    59.         {
    60.             dashingTime += Time.deltaTime;
    61.         }
    62.  
    63.         if (characterController.isGrounded && canDash == false && dashing >= dashLength)
    64.         {
    65.             canDash = true;
    66.             dashing = 0f;
    67.         }
    68.  
    69.         if (dashingTime >= dashResetTime && dashReset == false)
    70.         {
    71.             dashReset = true;
    72.             dashingTime = 0f;
    73.         }
    74.     }
    75. }