Search Unity

My character only dashes up

Discussion in 'Scripting' started by SuperCrow2, Mar 20, 2019.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    I don't know why. I didn't add anything new to the 2d platformer user controller script (just the default lines of code is there that comes with the character you get from the asset store (the robot guy). I will include both scripts, that script and my dash script.

    No matter what key on the keyboard I attach the other dashing directions to, it still doesn't work. I am not sure why only dashing up works.

    Dash:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. //makes character dash
    5.  
    6.  
    7. public class DashMove : MonoBehaviour
    8. {
    9.  
    10.     private Rigidbody2D rb;
    11.     public float dashSpeed;
    12.     private float dashTime; //how long the dash lasts
    13.     public float startDashTime; //decrease the value of dashTime in-game, so when dashTime is equal to 0 we know the dash is over
    14.     private int direction;
    15.  
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.         dashTime = startDashTime;
    20.     }
    21.     private void Update()
    22.     {
    23.         if (direction == 0)
    24.         {  //if it is, it means our player isn't dashing
    25.  
    26.             if (Input.GetKeyDown(KeyCode.Z))
    27.  
    28.                 direction = 1;
    29.         }
    30.  
    31.         else if (Input.GetKeyDown(KeyCode.X))
    32.         {
    33.  
    34.             direction = 2;
    35.  
    36.         }
    37.         else if (Input.GetKeyDown(KeyCode.C))
    38.         {
    39.             direction = 3;
    40.         }
    41.         else if (Input.GetKeyDown(KeyCode.V))
    42.         {
    43.             direction = 4;
    44.         }
    45.  
    46.         else
    47.         {
    48.             if (dashTime <= 0)//dashTime variable to decrease or the dash will never stop
    49.             {
    50.                 direction = 0;
    51.                 dashTime = startDashTime;
    52.                 rb.velocity = Vector2.zero; //or else the player will continue to dash foward
    53.             }
    54.             else
    55.             {
    56.                 dashTime -= Time.deltaTime;
    57.  
    58.                 if (direction == 1)
    59.                 {
    60.                     //dashes up
    61.                     rb.velocity = Vector2.up * dashSpeed;
    62.  
    63.                 }
    64.                 else if
    65.                   (direction == 2)
    66.                 {
    67.  
    68.                     rb.velocity = Vector2.right * dashSpeed;
    69.                 }
    70.                 else if
    71.              (direction == 3)
    72.                 {
    73.                     rb.velocity = Vector2.left * dashSpeed;
    74.                 }
    75.  
    76.                 else if
    77.                 (direction == 4)
    78.                 {
    79.                     rb.velocity = Vector2.down * dashSpeed;
    80.  
    81.                 }
    82.             }
    83.         }
    84.         }
    85.     }
    Character controller:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4.  
    5. namespace UnityStandardAssets._2D
    6. {
    7.  
    8.  
    9.  
    10.     [RequireComponent(typeof (PlatformerCharacter2D))]
    11.     public class Platformer2DUserControl : MonoBehaviour
    12.     {
    13.         private PlatformerCharacter2D m_Character;
    14.         private bool m_Jump;
    15.  
    16.  
    17.         private void Awake()
    18.         {
    19.             m_Character = GetComponent<PlatformerCharacter2D>();
    20.         }
    21.  
    22.  
    23.         private void Update()
    24.         {
    25.             if (!m_Jump)
    26.             {
    27.                 // Read the jump input in Update so button presses aren't missed.
    28.                 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    29.             }
    30.         }
    31.  
    32.  
    33.         private void FixedUpdate()
    34.         {
    35.             // Read the inputs.
    36.             bool crouch = Input.GetKey(KeyCode.LeftControl);
    37.             float h = CrossPlatformInputManager.GetAxis("Horizontal");
    38.             // Pass all parameters to the character control script.
    39.             m_Character.Move(h, crouch, m_Jump);
    40.             m_Jump = false;
    41.         }
    42.     }
    43. }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Looks like you're messed up with {}:

    Code (CSharp):
    1.        if (direction == 0)
    2.         {  //if it is, it means our player isn't dashing
    3.             if (Input.GetKeyDown(KeyCode.Z))
    4.                 direction = 1;
    5.         }
    6.         else if (Input.GetKeyDown(KeyCode.X))
    7.         {
    8.             direction = 2;
    9.         }
    In this code, the direction will become 2 only if player is already dashing (i.e. direction != 0). To fix that, you need to move all the input checks into first control block, where the Z key is checked.

    Code (CSharp):
    1.        if (direction == 0)
    2.         {  //if it is, it means our player isn't dashing
    3.  
    4.             if (Input.GetKeyDown(KeyCode.Z))
    5.                 direction = 1;
    6.             else if (Input.GetKeyDown(KeyCode.X))
    7.                 direction = 2;        
    8.             // etc...
    9. }
    Also your code will be much cleaner if you will use vectors for directions instead of integers, like this:

    Code (CSharp):
    1. direction = Vector3.up;
    2. rb.velocity = direction * dashSpeed;
     
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    I tried it. He still only dashes up for some reason.