Question by
lc4ll4ghan · Dec 01, 2020 at 07:29 PM ·
c#mobileunity 2d
How do I get my player to switch directions on a button held down by the user
So I'm just a beginner at unity and I'm building a project that you have to dodge balls falling from the sky and the character goes back and forth across the screen, but my problem is whenever the key button is held it doesn't work Here's the code if anyone has any idea of how to fix it using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;
public class SlowDownScript : MonoBehaviour {
private Rigidbody2D self;
public float moveSpeed;
public float switchTime;
public float changeSpeed;
private bool gameOver = false;
public int seconds = 0;
public Text clock;
void Start()
{
self = GetComponent<Rigidbody2D>();
InvokeRepeating("SwitchDirections", switchTime, switchTime * 2);
StartCoroutine(Count());
}
void UpdateClock()
{
seconds += 1;
clock.text = "Time: " + seconds;
}
void SwitchDirections()
{
moveSpeed *= -1;
}
void Update()
{
self.velocity = new Vector2(moveSpeed, -1f);
if (Input.GetMouseButtonDown(0))
{
Time.timeScale = 0f;
Time.timeScale = 1f;
}
}
void FixedUpdate()
{
self.velocity = new Vector2(moveSpeed, -1f);
if (Input.GetMouseButtonDown(0))
{
Time.timeScale = 0.5f;
Time.timeScale = 1f;
}
else
{
Time.timeScale = 1f;
Time.timeScale = 1f;
}
}
IEnumerator Count()
{
while (gameOver == false)
{
yield return new WaitForSecondsRealtime(1);
UpdateClock();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to make a follow camera code and jump sistem 1 Answer
Buttons for Player movement 0 Answers
LookAt in 2d is not working Again :/ 0 Answers