- Home /
FPShooter sideway/forward dash?
I'm in quite a pickle right now. My goal with this script is to make a sideways (right/left) and forward individual dashes. It contains multiple problems and I consider myself a complete noob when it comes to scripting. If someone has an alternative or solution of some sort I would be eternally grateful. The problems with this script are quite a few: (-)First when I rotate the camera the dashes don't move relatively to it, that dash's movement are always with relation the the X/Z world axis (-)Second, when i try to make the forward dash (Q and E keys simultaneously) it registers the 3 different dashes, left, right and forward. (-)Lastly my character is obviously affected by gravity, is there a way to disable it while I'm dashing.
//private var isDashing = false; was originally going to, but not using
var strength : int = 20;
var dashCost = 25;
private var canDash = true;
function Update ()
{
if(Master.StaminaPoints < 25 )
{
canDash = false;
}
else
{
canDash = true;
}
if(Input.GetKeyDown("e") && canDash == true)
{
Debug.Log("Dash Right");
gameObject.GetComponent(CharacterMotor).SetVelocity(Vector3.right * strength);
audio.Play();
Master.StaminaPoints = Master.StaminaPoints - dashCost;
}
if(Input.GetKeyDown("q") && canDash == true)
{
Debug.Log("Dash Left");
gameObject.GetComponent(CharacterMotor).SetVelocity(Vector3.left * strength);
audio.Play();
Master.StaminaPoints = Master.StaminaPoints - dashCost;
}
if(Input.GetKeyDown("q") && Input.GetKeyDown("e") && canDash == true)
{
Debug.Log("Dash Forward");
gameObject.GetComponent(CharacterMotor).SetVelocity(Vector3.forward * strength);
audio.Play();
Master.StaminaPoints = Master.StaminaPoints - dashCost;
}
}
Answer by Cherno · Sep 09, 2014 at 04:15 AM
Try using transform.right etc. instead of Vector3.right. If that doesn't help, look into the Character Motor script and see how movement is accomplished there.
Try calling the dashes with Input.GetKeyUp. Use GetKey to check if both Q and E are held down, and if yes, set a boolean "forwardDash" to true, and call the forward dash function. For the GetKeyUp check, add an if statement to check if "forwardDash" is set to false. If yes, then call either left or right dashing. If not, then only set dashing to false.
The CharacterMotor script has variables for Gravity and MaxFallSpeed.
Wow thank you Cherno!! I got the code to work. It's not perfect, I kind of got confused with your explanation about the forward dash but I think I did more less what you told me to. Anyway it works and I appreciate your answer. I'm gonna leave the code here just in case anyone ever comes across and needs it. private var isDashing = false; private var strength : int = 20; //var air = 10; //var ground = 30; var dashCost = 250; var forwardDash : boolean = false; private var canDash = true; private var dashLenght = .2;
function Start ()
{
isDashing = false;
Character$$anonymous$$otor$$anonymous$$ovement.gravity = 20;
}
function Update ()
{
if($$anonymous$$aster.Sta$$anonymous$$aPoints < 250 )
{
canDash = false;
}
else
{
canDash = true;
}
if(Character$$anonymous$$otor.grounded == true)
{
//Debug.Log ("ground");
strength = 30;
}
else
{
//Debug.Log ("air");
strength = 20;
}
if(Input.Get$$anonymous$$eyUp("e") && canDash == true && isDashing == false && forwardDash == false)
{
Right ();
}
if(Input.Get$$anonymous$$eyUp("q") && canDash == true && isDashing == false && forwardDash == false)
{
Left ();
}
if(Input.Get$$anonymous$$ey("q") && Input.Get$$anonymous$$ey("e") && canDash == true && isDashing == false)
{
Debug.Log ("Down");
forwardDash = true;
Forward ();
}
}
function Right ()
{
isDashing = true;
Debug.Log("Dash Right");
Character$$anonymous$$otor$$anonymous$$ovement.gravity = 0;
gameObject.GetComponent(Character$$anonymous$$otor).SetVelocity(transform.right * strength);
audio.Play();
$$anonymous$$aster.Sta$$anonymous$$aPoints = $$anonymous$$aster.Sta$$anonymous$$aPoints - dashCost;
yield WaitForSeconds(dashLenght);
Character$$anonymous$$otor$$anonymous$$ovement.gravity = 20;
isDashing = false;
}
function Left ()
{
isDashing = true;
Debug.Log("Dash Left");
Character$$anonymous$$otor$$anonymous$$ovement.gravity = 0;
gameObject.GetComponent(Character$$anonymous$$otor).SetVelocity(-transform.right * strength);
audio.Play();
$$anonymous$$aster.Sta$$anonymous$$aPoints = $$anonymous$$aster.Sta$$anonymous$$aPoints - dashCost;
yield WaitForSeconds(dashLenght);
Character$$anonymous$$otor$$anonymous$$ovement.gravity = 20;
isDashing = false;
}
function Forward ()
{
Debug.Log ("Forward");
//if(Input.Get$$anonymous$$eyUp("q") && Input.Get$$anonymous$$eyUp("e"))
//{
isDashing = true;
Debug.Log("Dash Forward");
Character$$anonymous$$otor$$anonymous$$ovement.gravity = 0;
gameObject.GetComponent(Character$$anonymous$$otor).SetVelocity(transform.forward * strength);
audio.Play();
$$anonymous$$aster.Sta$$anonymous$$aPoints = $$anonymous$$aster.Sta$$anonymous$$aPoints - dashCost;
yield WaitForSeconds(dashLenght);
Character$$anonymous$$otor$$anonymous$$ovement.gravity = 20;
isDashing = false;
//}
forwardDash = false;
}
Nice to know you got it to work. It always helps to just think about how something has to work, and map it out on good old notebook paper.
Pleaase flag the answer as accepted it it was indeed helpful :)
Your answer
Follow this Question
Related Questions
forward movement in camera direction (not direction of rigid body only) 3 Answers
Boost pad for character controller 0 Answers
Can't detect collision. 1 Answer
How do i on the y axis my camera from moving? 0 Answers
How to check for rapid key presses? 1 Answer