How to limit the amount of dashes in Unity 2020.2.0a15.1993?
Hello all! I am making a 2D top down shooter and I have a player that can move around the map, which is randomly generated. My player can dash around the map, but I want a limit for the amount of dashes in each level, for example: 3. How do I do it? Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//Dash
private bool isDashing;
public float dashDistance;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
//Dash
if (Input.GetMouseButtonDown(1))
{
isDashing = true;
}
}
private void FixedUpdate()
{
//Dash
if (isDashing)
{
rb.MovePosition(transform.position + (moveDirection * dashDistance));
isDashing = false;
}
}
}
All help will be appreciated. Thanks, Ahlam Hamid
Answer by CC_1759 · Aug 27, 2021 at 07:45 PM
@ahlamhamid123 put an int (ill call it timesDashed) and increment it when you dash. also put an if statement before GetMouseButtonDown which says if (timesDashed != 3). make a function that sets timesDashed to 0 and then in your script to go to another level refrence it. and if you want it to be able to set the amount of dashes they can do you can make another int (i called it dashLimit) this time public and replace the three in the if statement with it, and in the function for reseting timesDashed put one of these thing public void function(int whateverTheseAreCalledIDontRememer) and then set dashLimit to the paramiter in the function.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { //Dash public float dashDistance; public int dashLimit; bool isDashing; int timesDashed; private void Awake() { rb = GetComponent<Rigidbody2D>(); } private void Update() { //Dash if (Input.GetMouseButtonDown(1)) { isDashing = true; } } private void FixedUpdate() { //Dash if (timesDashed != dashLimit) { if (isDashing) { rb.MovePosition(transform.position + (moveDirection * dashDistance)); isDashing = false; dashLimit += 1; } } } public ResetDashLimit(int setAmountOfDashes) { dashLimit = setAmountOfDashes; timesDashed = 0; } }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//Dash
public float dashDistance;
public int dashLimit;
bool isDashing;
int timesDashed;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
//Dash
if (timesDashed != dashLimit)
{
if (Input.GetMouseButtonDown(1))
{
isDashing = true;
}
}
}
private void FixedUpdate()
{
//Dash
if (isDashing)
{
rb.MovePosition(transform.position + (moveDirection * dashDistance));
isDashing = false;
}
}
public void ResetDashLimit(int setAmountOfDashes)
{
dashLimit = setAmountOfDashes;
timesDashed = 0;
}
}
Your answer
Follow this Question
Related Questions
Cant get dash attack working 0 Answers
Blink, Dash, Raycasting 0 Answers
[2D] How to stop 2 vectors from adding force on each other 0 Answers
If statement being ignored 2 Answers
Cant Fix dash code in my movement script 0 Answers