Question by
lc4ll4ghan · Aug 18, 2020 at 10:34 AM ·
c#unity 2d
How can I slow down just my character when holding down on the Screen
Hi I need help with this project, I need to slow down a Player but when I implemented the code everything slowed down.
This is the code:
private Rigidbody2D self;
public float moveSpeed;
public float switchTime;
private bool gameOver = false;
private 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 FixedUpdate()
{
self.velocity = new Vector2(moveSpeed, 0f);
if (Input.GetMouseButton(0))
{
Time.timeScale = 0.5f;
}
else
{
Time.timeScale = 1f;
}
}
IEnumerator Count()
{
while (gameOver == false)
{
yield return new WaitForSecondsRealtime(1);
UpdateClock();
}
}
Comment
Answer by Oribow · Aug 18, 2020 at 10:33 AM
Why don't you half your moveSpeed value?
Time.timeScale is global. Leave it alone unless you want everything to slowdown or speed up.
@Oribow But now it leaves the screen, the slow down works now but the character just keeps going instead of moving from left to right every couple of seconds, i have concluded that its a problem with the
else { moveSpeed = 1f; }
string