Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 BrainUK · Jun 20, 2021 at 06:11 PM · unity 5javascriptplayer movementdash

Why am I only allowed to trigger my dash script once?

I have a player movement controller script and a dash script. The movement controller turns on the dash script whenever space is pressed but for some reason it only works once. It won't let me do it a second time. Can anyone tell me why?

Here's the movement controller:

 #pragma strict
 
 var originalMoveSpeed : float;
 var moveSpeed : float;
 var dashSpeed : float;
 var clampRadius : float;
 var walkSpeed : float;
 var isDashing : boolean;
 var rb2D : Rigidbody2D;
 var movement : Vector2;
 var playerObject : GameObject;
 
 function Update () {
     
     movement.x = Input.GetAxisRaw("Horizontal");
     movement.y = Input.GetAxisRaw("Vertical");
     
 }
 
 function FixedUpdate () {
     
     //normalize movement
     if (movement.magnitude > 1) {
         movement = movement.normalized;
     }
     
     //move
     rb2D.MovePosition(rb2D.position + movement * moveSpeed * Time.fixedDeltaTime);
     movement = Vector3.ClampMagnitude(movement, clampRadius);
     
     //dash
     if(Input.GetKeyDown(KeyCode.Space) && playerObject.GetComponent(playerDash).enabled == false) {
         playerObject.GetComponent(playerDash).enabled = true;
     }
     //walk
     if(Input.GetKey(KeyCode.LeftShift) == true && !Input.GetKeyDown(KeyCode.Space) == true) {
         moveSpeed = walkSpeed;
     }
     else {
         moveSpeed = originalMoveSpeed;
     }
     
     
 }
 

Here's the dash script:

 #pragma strict
 
 var dashSpeed : float;
 var dashTime : float;
 var isDashing : boolean;
 var playerObject : GameObject;
 
 function Start () {
     isDashing = true;
     playerObject = GameObject.Find("player");
     yield WaitForSeconds(dashTime);
     isDashing = false;
 }
 
 function Update () {
     
     if(isDashing == true) {
         playerObject.GetComponent(playerMovement).moveSpeed = dashSpeed;
     }
     
     else {
         playerObject.GetComponent(playerMovement).moveSpeed = playerObject.GetComponent(playerMovement).originalMoveSpeed;
         playerObject.GetComponent(playerDash).enabled = false;
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BrainUK · Jun 20, 2021 at 07:10 PM

Fixed it. Here's the working dash script:

 #pragma strict
 
 var dashSpeed : float;
 var dashTime : float;
 var isDashing : boolean;
 var playerObject : GameObject;
 
 function PlayerDash () {
     isDashing = true;
     playerObject = GameObject.Find("player");
     yield WaitForSeconds(dashTime);
     isDashing = false;
 }
 
 function OnEnable () {
     
     PlayerDash();
     
 }
 
 function Update () {
     
     if(isDashing == true) {
         playerObject.GetComponent(playerMovement).moveSpeed = dashSpeed;
     }
     
     else {
         playerObject.GetComponent(playerMovement).moveSpeed = playerObject.GetComponent(playerMovement).originalMoveSpeed;
         playerObject.GetComponent(playerDash).enabled = false;
     }
 }
Comment
Add comment · Share
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
avatar image
0

Answer by Nicoup · Jun 20, 2021 at 07:13 PM

It is triggered once because start method is called only once. Use OnEnable() instead.

Comment
Add comment · Share
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

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

257 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Ball Rolling Forever 0 Answers

Why does WASD move my player quicker than with a joystick? 1 Answer

How would I go about keeping my player object moving after a scene change? 3 Answers

GUI.Box NullReferenceException 0 Answers

Rigidbody.Addforce not working in Unity 5.4.1 3 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