Hello!
I’m new Unity and am trying to create a 2.5D infinite runner that has a jump and a dash. I’ve been trying different methods of getting the character to move and not sure how to proceed as I have 2 problems with the character dash.
-
If I use rigidbody.addforce my character dash works perfectly fine, but it takes way to long for the character to get up to the desired top speed when starting off from a standstill.
-
If I use rigidbody.velocity (the method I think I want to go with) using the same script to dash it does not increase the players speed.
Any help will be much appreciated ![]()
public class PlayerController : MonoBehaviour {
public bool onGround;
public float jumpForce;
public float moveForce;
public int layer;
private Rigidbody theRB;
private GameManager GMS;
// Use this for initialization
void Start () {
GMS = GameObject.Find ("Canvas").GetComponent<GameManager> ();
onGround = true;
theRB = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update ()
{
if (GMS.countDownDone == true) {
//theRB.AddForce (Vector2.right * moveForce);
theRB.velocity = new Vector2 (moveForce, theRB.velocity.y);
if (Input.GetButtonDown ("Jump") && onGround) {
if (jumpForce > 0f) {
//theRB.AddForce (Vector2.up * jumpForce, ForceMode.Impulse);
theRB.velocity = new Vector2 (theRB.velocity.x, jumpForce);
onGround = false;
}
}
}
}
void OnCollisionEnter(Collision collision){
if (collision.collider.gameObject.layer == LayerMask.NameToLayer("Default"))
{
onGround = true;
}
}
}
public class Dash : MonoBehaviour {
public DashState dashState;
public float dashCoolDown; //Dash cooldown if set to 0 cooldown is maxDash * 2(counts up to max dash then back down to 0)...... if you add a number into dashCoolDown.....maxDash * 2 + dashCoolDown.
public float maxDash = 20f; //How long player can dsah for.
public Vector2 savedVelocity; //Velocity of player before the dash...used to revert back to original velocity after dash is finished.
private Rigidbody theRB; //Reference to the players RigidBody.
void Awake () {
theRB = GetComponent<Rigidbody>();
}
void Update ()
{
switch (dashState)
{
//What happens before the dash.
case DashState.Ready:
if(Input.GetButtonDown("Fire3"))
{
//Initial velocity before the dash button is pressed.
savedVelocity = theRB.velocity;
//How FAST the player will dash.
theRB.velocity = new Vector2(theRB.velocity.x * 3f, theRB.velocity.y);
dashState = DashState.Dashing;
}
break;
//What happens during the dash.
case DashState.Dashing:
dashCoolDown += Time.deltaTime * 3;
if(dashCoolDown >= maxDash)
{
dashCoolDown = maxDash;
//Reverts back to initial velocity(speed before dash).
theRB.velocity = new Vector2(savedVelocity.x, theRB.velocity.y);
dashState = DashState.Cooldown;
}
break;
//What happens after the dash.
case DashState.Cooldown:
//Makes counter go down
dashCoolDown -= Time.deltaTime;
//Stops when timer hits 0
if(dashCoolDown <= 0)
{
//Won't let timer go below 0.
dashCoolDown = 0;
dashState = DashState.Ready;
}
break;
}
}
}
public enum DashState
{
Ready,
Dashing,
Cooldown
}