Double Tap Run

Updated on July 28, 2017 in  [G] Say Hello!
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on July 28, 2017

I want to make code the player walks faster if either key A or D is tapped twice. A (being left) and D (being right). Any suggestions on how I can do this.

Here’s my code:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Indingo : MonoBehaviour {

private Rigidbody2D MyRigidBody;

private Animator MyAnimator;

[SerializeField]
private float movementSpeed;

private bool facingRight;

// Use this for initialization
void Start ()
{
facingRight = true;
MyRigidBody = GetComponent<Rigidbody2D>();
MyAnimator = GetComponent<Animator>();
}

// Update is called once per frame
void FixedUpdate ()
{
float horizontal = Input.GetAxis(“Horizontal”);

HandleMovement(horizontal);

Flip(horizontal);
}

private void HandleMovement(float horizontal)
{
MyRigidBody.velocity = new Vector2(horizontal * movementSpeed, MyRigidBody.velocity.y); // x – 1, y = 0;

MyAnimator.SetFloat(“speed”, Mathf.Abs(horizontal));
}

private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;

Vector3 thescale = transform.localScale;

thescale.x *= -1;

transform.localScale = thescale;
}
}
}

  • Liked by
Reply