I'm trying to implement a limited-use slow-mo power into my game using C#, but I can use it infinitely. Why won't my integer go down when my function is called?
using UnityEngine;
public class TimeManager : MonoBehaviour
{
public float slowdownFactor = 0.05f;
public float slowdownLength = 2f;
public int slowmos = 5;
private void Update()
{
Time.timeScale += (1f / slowdownLength) * Time.unscaledDeltaTime;
Time.timeScale = Mathf.Clamp(Time.timeScale, 0f, 1f);
}
public void SlowMo()
{
if (slowmos != 0)
{
Time.timeScale = slowdownFactor;
Time.fixedDeltaTime = Time.timeScale * .02f;
slowmos--;
}
}
}
------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
------------------------------------------------------------------------
public class charcterMovement : MonoBehaviour
{
public TimeManager timemanager;
public Rigidbody rb;
public float forwardForce = 1000f;
public float rightForce = 100f;
public float leftForce = -100f;
// Start is called before the first frame update
void Start()
{
rb.useGravity = true;
}
// Update is called once per frame
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime, ForceMode.VelocityChange);
//Set Speed To Cube
if (Input.GetKey("d"))
{
rb.AddForce(rightForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(leftForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("space"))
{
timemanager.SlowMo();
}
}
}
SlowMo
an infinite number of times? I'd suggest you use a debugger and check where you update theslowmos
variable since you are probably resetting it somewhere (maybe through the Unity UI). \$\endgroup\$SlowMo()
, any code that sets the variableslomos
, and complete instructions for how to set up a scene in a new, empty project that demonstrates this issue? \$\endgroup\$