Why isn't speed changing?
Hey all, Pretty basic beginner at C# so I'm hoping it's a small thing that I've completely missed. I'm trying to do a 2D platformer with separate animations for walk and run. I've tried to set up a double tap feature to force the player to start running, except... it doesn't work.
What am I doing wrong?
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(h));
// Are we walking at full speed? If not, walk faster.
if (h * rb2d.velocity.x < maxWalking)
rb2d.AddForce(Vector2.right * h * moveForce);
// Are we walk faster than full speed? Slow down Sonic!!
if (Mathf.Abs(rb2d.velocity.x) > maxWalking)
rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxWalking, rb2d.velocity.y);
// Double Tap to Run
bool doubleTapRight = false;
#region doubleTapRight
if (Input.GetButtonDown("Horizontal"))
{
if (Time.time < doubleTapTime + .5f)
{
doubleTapRight = true;
}
doubleTapTime = Time.time;
}
#endregion
if (doubleTapRight)
{
Debug.Log("I am activating.");
anim.SetFloat("Speed", Mathf.Abs(h));
// Are we running at full speed? If not, run faster
if (h * rb2d.velocity.x < maxRunning)
rb2d.AddForce(Vector2.right * h * moveForce);
// Are we running faster than full speed? Slow down Usain Bolt!!
if (Mathf.Abs(rb2d.velocity.x) > maxRunning)
rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxRunning, rb2d.velocity.y);
}
Any help would be super appreciated! Thanks in advance :)
according to this: http://stackoverflow.com/questions/3151722/code-regions-not-allowed-within-method-bodies-in-vb-net
The problem can might be with the region if you are using an older verion of Visual studio.
You can might try to remove the double tap region to check if it works, or a simple Debug.Log within the input.getbuttondown("horizontal");
Hey JScotty, thanks for the quick reply! :)
So, I placed the Debug.Log within the region, tested, and removed the region, tested again. The Log returns a result on a single tap which is interesting as the old Debug that's in the code above returns on the double tap.
Answer by AngryKiwiArts · Nov 03, 2016 at 12:10 PM
this looks like remnants of an era long passed from DataLohr https://forum.unity3d.com/threads/single-tap-double-tap-script.83794/#post-1401444
try separating the double tap code and see if you can get the bool to change.
Hey Angry$$anonymous$$iwiArts, Yeah, I've tried adapting DataLohr's code from that exact thread. So, I've separated the code and the debug still triggers but I'm beginning to wonder if it's how I've set up motion for the controller.
The controller limits the speed of the character if he is traveling too fast. Perhaps, the initial maxWalking is interfering with the double tap?
Your answer
Follow this Question
Related Questions
Remember position for a simple player controller (Left/Right)? 0 Answers
Smooth No Gimbal Lock Analogue Rotation 1 Answer
How would do I play a walking animation when i move my joystick? 0 Answers
How can we control two player objects at one time with RayCasting? 1 Answer
oculus go movement 0 Answers