- Home /
Question by
Beanmeister12 · May 21, 2018 at 07:49 PM ·
c#movementrigidbody2d
Inconsistent Rigidbody?
The code I am using for a dash move in my game is giving inconsistent results. Sometimes, the dashing goes exactly as far as its supposed to go, but other times it goes really far. The inconsistent results are only for the left and right dashes.
public class Player_Movement : MonoBehaviour {
void Movement () {
//Dashing
if (Input.GetKeyDown (KeyCode.F) && isGrounded == true && cA == false && hasDashed == false) {
//Dashes right if moving right
if (moveX > 0.15)
{
hasDashed = true;
player.AddForce (Vector2.right * dashPowerHorz, ForceMode2D.Impulse);
dashCollecterLeft.SetActive (true);
StartCoroutine (DashReset ());
}
//Dashes left if moving left
if (moveX < -0.15)
{
hasDashed = true;
player.AddForce (Vector2.left * dashPowerHorz, ForceMode2D.Impulse);
dashCollecterRight.SetActive (true);
StartCoroutine (DashReset ());
}
//Dashes up if still
if (moveX == 0)
{
hasDashed = true;
player.AddForce (Vector2.up * dashPowerVert);
StartCoroutine (DashReset ());
}
//Makes player unable to dash again
isGrounded = false;
}
}
void OnCollisionEnter2D (Collision2D col) {
if (col.gameObject.tag == "ground") {
jumpTimes = 0;
isGrounded = true;
}
}
void OnCollisionStay2D (Collision2D col)
{
if (col.gameObject.tag == "ground")
{
isGrounded = true;
}
}
//Turns grounded off
void OnCollisionExit2D (Collision2D col)
{
if (col.gameObject.tag == "ground")
{
isGrounded = false;
}
}
void Update () {
Movement ();
}
IEnumerator DashReset () {
yield return new WaitForSeconds (0.1f);
hasDashed = false;
dashCollecterLeft.SetActive (false);
dashCollecterRight.SetActive (false);
}
}
Comment