- Home /
dash left and right
okay, so I have compiled a script to make a character dash, both midair and on ground, but the character can only dash forward. I would like for the character to be able to dash both forward and backward.
Forward if I simply press the dash button (which I've assigned to the x key)
Backward if I press dash while holding down the left arrow key. This is part of what I came up with (only the pertinent stuff though)
var speed = 6;
var jumpSpeed = 8;
var jumpCount = 0;
var maxJump = 2;
var dashSpeed = 100.0;
var dashCount = 0;
var maxDash = 1;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function update () {
//allow free range horizontal movement while in air
if (!grounded) {
moveDirection.x = Input.GetAxis("Horizontal")*speed;
//air dash one time only
if (dashCount < maxDash) {
if (Input.GetKey (KeyCode.X)) {
moveDirection.x = dashSpeed;
dashCount++;} } }
else {moveDirection = Vector3(Input.GetAxis("Horizontal")*speed, 0.0, Input.GetAxis("Vertical")*speed);
//reset jump because we are grounded
jumpCount = 0;
//ground dash once
if (dashCount < maxDash) { if (Input.GetKeyDown (KeyCode.X)) { moveDirection.x = dashSpeed; dashCount++;}
//reset dash upon key release
else if (Input.GetKeyUp (KeyCode.X))
{dashCount = 0;}}
//reset dash when used midair (otherwise you would need to press and release
X again; if you simply reset dash when on ground you end up dashing forever
until you fall off platform
else {dashCount = 0;}
}
okay so that's what I have, the character only wants to dash forward because dashSpeed is a positive integer, and the arrow keys don't reflect positive or negative values. I'm not sure what I need to do to tweak it, but if there is some sort of scripting knowledge I'm unaware of I would love to hear lol. Thank you!
Answer by shane.rachel · Nov 07, 2012 at 12:19 AM
I had to use this: if (Input.GetKey (KeyCode.X) && Input.GetKey (KeyCode.LeftArrow)){ moveDirection = dashSpeed*-1;}
the function works now, thanks anyways though lol
Your answer
Follow this Question
Related Questions
How do you double jump in a 2d game 1 Answer
why will my jump animation only play on multi-jumps? 0 Answers
problems with grounded and double jumping 2 Answers
Character Jump Problem! 0 Answers