Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jan 29, 2019 at 02:19 PM by frontlinesaint for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by frontlinesaint · Jan 25, 2019 at 11:39 PM · 2d game2d-platformer2d-physics

2d platformer script problems

Hi i am new to programming and this probably is not the best script for what im trying to achieve but i have it kind of functioning. I am trying to make a 2d platformer game.

In this script i have to use fixed update because thats the only way i can get it to work, but some times when i press the dash key its not fully responsive is there a reason why?

Ideally i would like to have my player dash once, left, right and diagonally and have a cool down time so cant keep dashing until my player is grounded again but due to my skills that limits me alot. If anyone could help me with a better way that would be hugely appreciated but i guess people are busy.

Or even if someone can set me on the right track to acheive what im after that would be great! thanks heres the script:

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

 public class DashAbility : MonoBehaviour
 {
 
     public DashState dashState;
     public float dashTimer;
     public float maxDash = 20f;
 
 
     public float dashSpeed;
     public float returnMovementSpeed;
 
     public bool dashingRight;
     public bool dashingLeft;
 
 
     Rigidbody2D rb;
     PlayerController playerControllerScript;
 
 
 
      void Start()
     {
         rb = GetComponent<Rigidbody2D>();
 
         playerControllerScript = GetComponent<PlayerController>();
 
         returnMovementSpeed = playerControllerScript.speed;
         
     }
 
     private void FixedUpdate()
     {
         switch (dashState)
         {
             case DashState.Ready:
                 var isDashKeyDown = Input.GetKeyDown(KeyCode.LeftShift);
                 if (isDashKeyDown && playerControllerScript.facingRight == true)
 
                 {
                     dashState = DashState.Dashing;
                     dashingRight = true;
                     dashingLeft = false;
                 }
 
                 if (isDashKeyDown && playerControllerScript.facingRight == false)
 
                 {
                     dashState = DashState.Dashing;
                     dashingRight = false;
                     dashingLeft = true;
                 }
                 break;
             case DashState.Dashing:
                 dashTimer += Time.deltaTime;
                 if (dashTimer >= maxDash && dashingRight == true)
                 {
                     dashTimer = maxDash;
                     rb.velocity = transform.right * dashSpeed;
                     playerControllerScript.speed = 0f;
                     dashingLeft = false;
                     dashState = DashState.Cooldown;
 
                 }
                 if (dashTimer >= maxDash && dashingLeft == true)
                 {
                     dashTimer = maxDash;
                     rb.velocity = -transform.right * dashSpeed;
                     playerControllerScript.speed = 0f;
                     rb.gravityScale = 0;
                     dashingRight = false;
                     dashState = DashState.Cooldown;
                 }
 
 
 
                 break;
             case DashState.Cooldown:
                 dashTimer -= Time.deltaTime;
                 if (dashTimer <= 0)
                 {
                     dashTimer = 0;
                     dashingRight = false;
                     dashingLeft = false;
                     rb.gravityScale = 4;
                     playerControllerScript.speed = returnMovementSpeed;
 
                     dashState = DashState.Ready;
                 }
                 break;
         }
     }
 }
 
 public enum DashState
 {
     Ready,
     Dashing,
     Cooldown
 }
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

  • Sort: 

Follow this Question

Answers Answers and Comments

121 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

Related Questions

I need to shoot a projectile in my 2D platformer in the opposite direction my player is dashing which can be in any direction the player points the joystick 2 Answers

Having some stuck issues on the 2D infinite runner 0 Answers

Unity crashes 0 Answers

Way to achieve 3 lane mechanism in a 2D game 0 Answers

HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers


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