Dashing cube ability problem
Hello, I've got a problem with creating a dash ability in my game (3d)
I've currently got it so that when you double press D, I add velocity in that direction. Here comes the problem, I've given the player a material that has 0 friction. Otherwise you would be able to jump in walls and kinda get stuck in them. Now you just glide down when jumping against a wall.
Now, when I add velocity, It just keeps going, and doesn't stop. Because there is nothing to stop the velocity since there is no friction. I've given the player's rigidbody drag, so that there is some form of friction. but its still not working as I would like.
I've tried to add more drag while dashing, but if I give the player more drag, it falls down more slowly too, which I don't want.
This is my current code:
public float xSpeed = 1;
public float zSpeed = 1;
public bool Grounded = false;
public bool reset;
public bool firstButtonPressed;
public float timeOfFirstButton;
public float DashRightSpeed = 10;
public float HoldSpeedRight;
public float dashTimer = 3f;
public float dragger = 1;
public Rigidbody rb;
Vector3 CurrentVelocity;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
//rb.drag = 20;
}
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "ground") {
Grounded = true;
}
}
// Update is called once per frame
void Update () {
CurrentVelocity = rb.velocity;
//Activate Dash
if(Input.GetKeyDown(KeyCode.D) && firstButtonPressed) {
//firstButtonPressed = false;
if(Time.time - timeOfFirstButton < 0.5f && dashTimer >= 3) {
firstButtonPressed = false;
dashTimer = 0;
Debug.Log("DoubleClicked");
dragger = 3;
rb.velocity = new Vector3(10, CurrentVelocity.y, CurrentVelocity.z);
}
}
//First button press
else if(Input.GetKeyDown(KeyCode.D) && !firstButtonPressed) {
firstButtonPressed = true;
timeOfFirstButton = Time.time;
}
// Second button press too slow, reset First press bool
if (firstButtonPressed) {
if (Time.time - timeOfFirstButton > 0.5f) {
firstButtonPressed = false;
}
}
rb.drag = dragger;
if (dragger > 1) {
dragger = dragger - Time.deltaTime * 4;
}
// Reset timer for dash
if (dashTimer < 3) {
dashTimer += Time.deltaTime;
}
Holder.xPos = transform.position.x.ToString();
Holder.zPos = transform.position.z.ToString ();
}
void FixedUpdate(){
float x = Input.GetAxis ("Horizontal") * xSpeed;
float z = Input.GetAxis ("Vertical") * zSpeed;
transform.Translate (x, 0, z);
if (Input.GetKey(KeyCode.Space) && Grounded) {
Grounded = false;
rb.velocity = new Vector3(CurrentVelocity.x, 10, CurrentVelocity.z);
}
}
I'm sorry for the spaghetti code, If anyone knows how i could get this to work, I would be really grateful.
Example of dashing: Here
Your answer
Follow this Question
Related Questions
Move rigidbody cube without it tumbling 2 Answers
Cant figure out simple camera orbit 0 Answers
how to create dress up scene in unity? 0 Answers