I want to make it so you move in a direction as long as you hold the button.
But i dont know how to make that happen.
I tried looking it up but the tutorials i followed didnt work
I think i need to somehow link the button hold with the D key, or something like that, basically when i hold down the button it registers like i would hold down D key or A key.
This is the code.
using UnityEngine;
using System;
public class PlayerMovement : MonoBehaviour
{
// Movement
public float speed = 1;
float XLimit;
// Player death
public event Action OnPlayerDeath;
public GameObject GameOverMenu;
public GameObject Player;
// Charging arrows - UI
public GameObject TopImageUC;
public GameObject MiddleImageUC;
public GameObject BottomImageUC;
public GameObject TopImageC;
public GameObject MiddleImageC;
public GameObject BottomImageC;
int ArrowToCharge = 3;
// Dash
public float AbilityCooldown = 45f;
float AbilityCurrentCooldown;
float Dividor;
float Charging;
string lastKeyPressed = "";
float WaitForSecondsA = 0.01f;
float WaitForSecondsD = 0.01f;
bool ChargingToA = false;
bool ChargingToD = false;
bool dashButtonPressed = false;
public bool AbilityReady = false;
// Dash effects
public Animator DashAnimation;
public ParticleSystem Trail;
void Start()
{
float PlayerHalf = transform.localScale.x / 2f;
XLimit = Camera.main.aspect * Camera.main.orthographicSize + PlayerHalf;
AbilityCurrentCooldown = AbilityCooldown;
Dividor = AbilityCooldown;
Charging = AbilityCooldown;
}
// Charging arrows - UI ----------------------------------------------------------------------------------------------------
void ChargeOneByOne()
{
if (ArrowToCharge == 3)
{
BottomImageUC.SetActive(false);
BottomImageC.SetActive(true);
}
if (ArrowToCharge == 2)
{
MiddleImageUC.SetActive(false);
MiddleImageC.SetActive(true);
}
if (ArrowToCharge == 1)
{
TopImageUC.SetActive(false);
TopImageC.SetActive(true);
}
ArrowToCharge = ArrowToCharge - 1;
}
void Update()
{
//Movement ----------------------------------------------------------------------------------------------------
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
Vector3 distance = input.normalized;
Vector3 velocity = distance * speed;
Vector3 moveAmount = velocity * Time.deltaTime;
//Movement Speed
if (speed <= 10)
speed = speed + 0.04f * Time.deltaTime;
//Teleport if out of screen
if (transform.position.x < -XLimit)
{
transform.position = new Vector3(XLimit, transform.position.y, transform.position.z);
}
if (transform.position.x > XLimit)
{
transform.position = new Vector3(-XLimit, transform.position.y, transform.position.z);
}
// Ability ----------------------------------------------------------------------------------------------------
// Charging indicator - UI
if (AbilityCurrentCooldown <= Charging - Dividor / 3)
{
Charging = Charging - Dividor / 3;
ChargeOneByOne();
}
// Cooldown
AbilityCurrentCooldown = AbilityCurrentCooldown - Time.deltaTime;
if (AbilityCurrentCooldown <= 0f)
{
AbilityReady = true;
}
// Dash ------------------------------
// Remembers which key out of A and D was pressed last.
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D) == false)
{
lastKeyPressed = "A";
}
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A) == false)
{
lastKeyPressed = "D";
}
if (Input.GetKeyDown(KeyCode.Space))
{
dashButtonPressed = true;
}
// Dash on space press ( Right dash, D key )
if (AbilityReady == true && GameOver.GameEnded == false && dashButtonPressed == true && lastKeyPressed == "D" && Input.GetKey(KeyCode.A) == false)
{
dashButtonPressed = false;
ChargingToD = true;
WaitForSecondsD = Time.time;
DashAnimation.Play("PlayDash");
Trail.Play();
}
if (ChargingToD == true && Time.time > WaitForSecondsD)
{
ChargingToD = false;
transform.Translate(3.5f, 0, 0);
AbilityCurrentCooldown = AbilityCooldown;
Charging = AbilityCooldown;
ArrowToCharge = 3;
AbilityReady = false;
TopImageC.SetActive(false);
MiddleImageC.SetActive(false);
BottomImageC.SetActive(false);
TopImageUC.SetActive(true);
MiddleImageUC.SetActive(true);
BottomImageUC.SetActive(true);
}
// Dash on space press ( Left dash, A key )
if (AbilityReady == true && GameOver.GameEnded == false && dashButtonPressed == true && lastKeyPressed == "A" && Input.GetKey(KeyCode.D) == false)
{
dashButtonPressed = false;
ChargingToA = true;
WaitForSecondsA = Time.time;
DashAnimation.Play("PlayDash");
Trail.Play();
}
if (ChargingToA == true && Time.time > WaitForSecondsA)
{
ChargingToA = false;
transform.Translate(-3.5f, 0, 0);
AbilityCurrentCooldown = AbilityCooldown;
Charging = AbilityCooldown;
ArrowToCharge = 3;
AbilityReady = false;
TopImageC.SetActive(false);
MiddleImageC.SetActive(false);
BottomImageC.SetActive(false);
TopImageUC.SetActive(true);
MiddleImageUC.SetActive(true);
BottomImageUC.SetActive(true);
}
}
// Player death on collision ----------------------------------------------------------------------------------------------------
void OnTriggerEnter(Collider triggerCollider)
{
if (triggerCollider.tag == "Enemy")
{
if (OnPlayerDeath != null)
{
OnPlayerDeath();
}
Destroy(gameObject);
}
}
public void DashOnClick()
{
if (AbilityReady == true)
{
dashButtonPressed = true;
}
}
}