Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SuperCrow2 · Jun 27, 2020 at 06:11 AM · 2d gameunity 2dplatformercodepagedash

How do I get my character to dash up?

I can only get him to dash left and right, idk how to get him to dash up. I dont think you have to sift through the 500+ line code, I tried to make it super quick and easy for everyone by putting the most important lines, the ones that pertains to the dash below. Ignore the commented out stuff, I had to keep that in there when I posted the script here so I wouldnt confuse myself when it comes time to making the up dash work. The commented out dash stuff you may need.

The lines or sections that has the dash: 142 213 224 247 261

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class WizardPlayerController : MonoBehaviour
 {
 
     private Renderer rend;
     private Color colorToTurnTo = Color.blue;
 
 
     public float spead;
     public float jumpForce = 16.0f; //how high the character jumps
     private float movementInputDirection; //the key being pressed down
     private float dashTimeLeft; //keep track how much longer the dash should be happening 
     private float lastImageXpos; //keep track on x coordinate of after image
     private float lastDash = -100; //last time we started dash, will be used to check for cool down.  -100 so we can dash right when game starts
 
     private float timeLeft; //4 seconds to be off the ground, at the end, drop back down
 
     private float timeLeftSet = 4f; //set our timeLeft at 4 seconds 
 
     private float jumpTimer;
 
     private float turnTimer;
 
     private float wallJumpTimer; //this will be part of the code to prevent jumping up a single wall
 
 
     private int facingDiection = 1; //-1 is left.  1 is right
 
 
     private int amountOfJumpsLeft;
 
     private int lastWallJumpDirecion; //this will be part of the code to prevent jumping up a single wall
 
     private bool canMove;
 
     private Rigidbody2D rb;
 
     private bool canwallGrab;
 
     private bool isDashing;
 
     private bool dashUp;
 
     private bool facingRight = true;
 
     private bool isGrounded;
 
     private bool canNormalJump;
 
     private bool canWallJump;
 
     private bool isAttemptingToJump;
 
     private bool canFlip;
 
     private bool hasWallJumped; //this will be part of the code to prevent jumping up a single wall
 
     private bool isTouchingWall;
 
     private bool isWallSliding;
 
     private bool checkJumpMultiplier;
 
     public Transform groundCheck;
     public float checkRadius;
     public LayerMask whatIsGround;
 
     private float moveHorizontal;
     private float moveVertical;
     public float dashTime; //how long the dash should take
     public float dashSpeed; //how fast it moves while dashing
     public float distanceBetweenImages; //how far the after image game objects should be placed when dashing (the dash effect, it creates images behind him when he dashes)
     public float dashCoolDown; //how long till we can dash again
     public float movementSpeed = 10.0f;
 
     public int amountOfJumps = 1;
 
     public float wallSlideSpeed;
 
     public float movementForceInAir; //add force to character as its attempting to move in the air
 
     public float airDragMultiplier = 0.95f;
 
     public float variableJumpHeightMultiplier = 0.5f; //tapping space jumps lower than a full press
 
     public Vector2 wallHopDirection;
 
     public Vector2 wallJumpDirection;
 
     public float wallHopForce;
 
     public float wallJumpFoce; 
 
     public float jumpTimerSet = 0.15f;
 
     public float turnTimerSet = 0.1f;
 
     public float wallJumpTimerSet = 0.5f; //this will be part of the code to prevent jumping up a single wall
 
     public float groundCheckRadius;
 
     public float wallCheckDistance;
 
     public Transform wallCheck;
 
     private Vector2 speed;
 
     SavePlayerPosition playerPositionData;
 
     //-----------------------------------------------------------------------------
     private void Awake()
     {
         playerPositionData = FindObjectOfType<SavePlayerPosition>(); //finding the script
         playerPositionData.PlayerPosLoad();
     }
 
 
 
 
 
     //--------------------------------------------------------------
     void Start()
     {
 
         rb = GetComponent<Rigidbody2D>();
         wallHopDirection.Normalize(); //vector equals 1.  when we specify a force, it will always be the force we speified
         wallJumpDirection.Normalize();
         amountOfJumpsLeft = amountOfJumps;
     }
 
     //---------------------------------------------------------
 
     private void Update()
     {
         CheckInput();
         CheckMovementDirection();
         CheckIfCanJump();
         CheckIfWallSliding();
         CheckDash();
         CheckJump();
         TimeInAir();
         WallGrab();
 
     }
 
     //-----------------------------------------------------
 
     private void CheckIfWallSliding()
     {
         if (isTouchingWall && movementInputDirection == facingDiection && rb.velocity.y < 0)  //rb.velocity.y<0 so our character slides down only when going down not when jumping up the wall
         {
             isWallSliding = true;
         }
         else
         {
             isWallSliding = false;
         }
     }
 
 
     //---------------------------------------------------------------------------------
     private void CheckInput()
     {
 
         movementInputDirection = Input.GetAxisRaw("Horizontal");
 
         if (Input.GetButtonDown("Jump"))
         {
             if (isGrounded || (amountOfJumpsLeft > 0 && isTouchingWall))
             {
                 NormalJump();
             }
 
 
             else
             {
                 jumpTimer = jumpTimerSet;
                 isAttemptingToJump = true;
             }
         }
 
 
         if (Input.GetButtonDown("Horizontal") && isTouchingWall)
         {
             if (!isGrounded && movementInputDirection != facingDiection)
             {
                 canMove = false;
                 canFlip = false;
 
                 turnTimer = turnTimerSet;    //how long to keep it false
             }
         }
 
         if (!canMove)
         {
             turnTimer -= Time.deltaTime;
 
             if (turnTimer <= 0)
             {
                 canMove = true;
                 canFlip = true;
             }
         }
 
         if (checkJumpMultiplier && !Input.GetButton("Jump")) //space bar.  pushing spacebar it will return true.  if not pressing it, it will return false
         {
             checkJumpMultiplier = false;
             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * variableJumpHeightMultiplier); //tapping the jump key he jumps lower
         }
         if (Input.GetButtonDown("Dash")) //z button
         {
             if (Time.time >= (lastDash + dashCoolDown)) //only dash once cool down is up we can dash again
                 AttemptToDash();
         }
     }
 
 
 
     //----------------------------------------------------------------------------------
 
     private void AttemptToDash() //start of the dash
     {
         isDashing = true;
         //dashUp = true;
         dashTimeLeft = dashTime;
         lastDash = Time.time;
 
 
 
         //  }
         // else
         //  {
         //     dashUp = false;
 
 
         //  }
 
         PlayerAfterImagePool.Instance.GetFromPool();
         lastImageXpos = transform.position.x;
     }
 
     //___________________________________________________________________
 
     private void DashUp()
     {
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             dashUp = true;
             dashTimeLeft = dashTime;
             lastDash = Time.time;
             PlayerAfterImagePool.Instance.GetFromPool();
             lastImageXpos = transform.position.y;
         }
     }
 
  //---------------------------------------------------------------------------
 
     private void CheckDash()
 {
         if (isDashing)
         {
 
       if (dashTimeLeft > 0)
             {
 
                 canMove = false;
                 canFlip = false;
 
                 rb.velocity = new Vector2(dashSpeed * facingDiection, 0); //facingDirecion,0 is to get him to dash straight (which is what I wanted to happen).  using rb.velocity.y caused him to dash up on a curve like thing
                 dashTimeLeft -= Time.deltaTime;
             }
 
             // if (dashUp == true) //this "if statement" to get him to dash vertically )
 
             //    {
             //   rb.velocity = new Vector2(rb.velocity.x * facingDiection, dashSpeed);
 
            
              if (Mathf.Abs(transform.position.x - lastImageXpos) > distanceBetweenImages)
                 {
                     PlayerAfterImagePool.Instance.GetFromPool();
                     lastImageXpos = transform.position.x;
                 }
 
             }
         if (dashTimeLeft <= 0 || isTouchingWall)
         {
             isDashing = false;
             canMove = true;
             canFlip = true;
         }
 
     }
 
 //------------------------------------------------------
 
 private void CheckJump() //I changed jump() to CheckJump()
 {
 
     if (jumpTimer > 0)
     {
         if (!isGrounded && isTouchingWall && movementInputDirection != 0 && movementInputDirection != facingDiection)
         {
             WallJump(); //wall jum[
         }
 
         else if (isGrounded)
         {
             NormalJump();
         }
     }
      if(isAttemptingToJump)
     {
     jumpTimer-=Time.deltaTime;
 }
      if(wallJumpTimer>0)
         {
             if(hasWallJumped && movementInputDirection== -lastWallJumpDirecion)
             {
                 rb.velocity = new Vector2(rb.velocity.x, 0.0f);
                 hasWallJumped = false;
             } else if(wallJumpTimer<=0)
             {
                 hasWallJumped = false;
             }
             else
             {
                 wallJumpTimer -= Time.deltaTime;
             }
         }
 
     }
         
    private void NormalJump()  //this and the WllJump() section was combined in the Jump() but now called CheckJump()
     {
         if (canNormalJump) 
         {
           rb.velocity = new Vector2(rb.velocity.x, jumpForce); //jumpforce is our y velocity
             amountOfJumpsLeft--;
            jumpTimer = 0;
         isAttemptingToJump = false;
         checkJumpMultiplier = true;
     }
 
     }
 
     private void WallJump()
     {
            if (canWallJump)      //wall jumping
             {
             rb.velocity = new Vector2(rb.velocity.x, 0.0f);
             isWallSliding = false;
             amountOfJumpsLeft = amountOfJumps;
             amountOfJumpsLeft--; //subtract from amount of jumps left (i may have to keep this rmeoved, it was causing him to run out of jumps so he couldnt jump anymore
             Vector2 forceToAdd = new Vector2(wallJumpFoce * wallJumpDirection.x * movementInputDirection, wallJumpFoce * wallJumpDirection.y);
             rb.AddForce(forceToAdd, ForceMode2D.Impulse);
             jumpTimer = 0;
             isAttemptingToJump = false;
             checkJumpMultiplier = true;
             turnTimer = 0;
             canMove = true;
             canFlip = true;
             hasWallJumped = true;
             wallJumpTimer = wallJumpTimerSet;
             lastWallJumpDirecion = -facingDiection;
     }
 
     }
 
     private void FixedUpdate()
     {
 
         ApplyMovement();
         CheckSurroundings();
     }
     //------------------------------------------------------------
 
     private void CheckSurroundings()
     {
         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
 
         isTouchingWall = Physics2D.Raycast(wallCheck.position, transform.right, wallCheckDistance, whatIsGround);
     }
 
 
     //---------------------------------------------------------------------------
 
 
         private void WallGrab()
     {
         if(canwallGrab)
         {
             timeLeft -= Time.deltaTime;
         }
     }
 
 
 
     private void TimeInAir() //the time he's not on the ground
     {
 
   }
 
   private void CheckIfCanJump() //check to see if he can jump
     {
         if (isGrounded && rb.velocity.y <= 0.01f) 
         {
             amountOfJumpsLeft = amountOfJumps;
 
         }
 
         if (isTouchingWall)
         {
             canWallJump = true;
         }
 
         if (amountOfJumpsLeft <= 0)
         {
             canNormalJump = false;
         }
 
         else
 
         {
             canNormalJump = true;
         }
     }
 
     //--------------------------------------------------------------------------
 
 
 
     private void ApplyMovement()
     {
         if (!isGrounded && !isWallSliding && movementInputDirection == 0) //when we jump straight up, we can't move until it touches ground.  //I asusme when movementInoutDirecion==0 that means hes not moving in any direction
         {
             rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
         }
 
         else if (canMove)
         { //apply the force
 
             rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
         }
 
         //Vector2 forceToAdd = new Vector2(movementForceInAir * movementInputDirection, 0);
         //rb.AddForce(forceToAdd);
 
 
         if (isWallSliding)
         {
             if (rb.velocity.y < -wallSlideSpeed)
             {
                 rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
             }
         }
     }
 
         //if (Mathf.Abs(rb.velocity.x) > movementSpeed)
             //{
             //    rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
           //  }
         //}
 
          //I assume when movementInoutDirecion==0 that means hes not moving in any direction
        // {
      //       rb.velocity = new Vector2(rb.velocity.x * airDragMultiplier, rb.velocity.y);//keep y velocicty the same.  //slows the guy down
    //     }
     
         //if (isWallSliding)
        // {
             //if (rb.velocity.y < -wallSlideSpeed)
             //{
           //      rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
       ////      }
     //    }
   //  }
 
    
 //(!isGrounded && !isWallSliding && movementInputDirection != 0) //not is grounded and not is wall sliding
 //----------------------------------------------------------------------------------
 
     private void CheckMovementDirection() //moves him right or left.  flips him so he faces the direction he is going in
     {
 
         if (facingRight && movementInputDirection < 0)
         {
 
             Flip();
 
         }
 
         else if (!facingRight && movementInputDirection > 0) //because he is moving left while facing right
         {
             Flip();
         }
 
     }
 
 
     void ChangeColor()
     {
 
         if (dashCoolDown<=2.0f)
         {
             rend.material.color = colorToTurnTo;
         }
     }
 
 
     private void Flip()
 
     {
      if (!isWallSliding && canFlip)
         
           {
             facingDiection *= -1;  //facing left.  every time we flip the character it will go back and forth between -1 and 1
             facingRight = !facingRight;
             transform.Rotate(0.0f, 180.0f, 0.0f); //x, y, z
         }
         
     }
 
     private void OnDrawGizmos()
     {
         Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
         Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance, wallCheck.position.y, wallCheck.position.z));
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

170 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D platformer - character clips into the ground 1 Answer

Why does my sprite change size when in game but not when in scene? 0 Answers

Hi I am making a 2D game and I am having problem with this script: 1 Answer

Spawning mobs in a platformer game 0 Answers

Unity 2D - Use Digital Painting as Terrain 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges