0
\$\begingroup\$

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();
        }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ What's the problem? You can call SlowMo an infinite number of times? I'd suggest you use a debugger and check where you update the slowmos variable since you are probably resetting it somewhere (maybe through the Unity UI). \$\endgroup\$
    – Charanor
    Commented Sep 28, 2020 at 2:53
  • \$\begingroup\$ I'm unable to reproduce the problem with this code alone. Can you show us the code that calls SlowMo(), any code that sets the variable slomos, and complete instructions for how to set up a scene in a new, empty project that demonstrates this issue? \$\endgroup\$
    – DMGregory
    Commented Sep 28, 2020 at 3:20
  • \$\begingroup\$ It is now followed by the code that calls the function \$\endgroup\$ Commented Sep 28, 2020 at 3:44

1 Answer 1

1
\$\begingroup\$

There are a few problems, lets go over them one by one.

For the start, don't use if (slowmos != 0). If for some reason your slowmos drops to negative, you have infinite slowmos since it is never 0 and you happily subtract just another slowmo. Better is to just check if you have more than 0 if (slowmos > 0).

The next problem is if (Input.GetKey("space")). GetKey returns true as long as it is pressed, as long as it is pressed. With 60fps your slowmos are gone like instant. Together with the first problem, it could be reduced into negative values. Use if (Input.GetKeyDown("space")) instead. This returns once true so your slowmo is only called on the first space pressed.

Now that we use the correct method, it needs to move to the correct update method. While fixedUpdate is correct for physic manipulation, if you leave the new GetKeyDown there, it won't always capture your pressed key events. You did not had the problem with skipped key input, since your GetKey was spamming slowmo with USE IT, USE IT.

Last thing is, you should take a look at the input manager from unity. It allows you easier bindings of keys (maybe user want to remap later the slowmo to not space but ctrl - whatever). The code would change to Input.GetButtonDown("SlowMo"). You need to define SlowMo in the input manager, switch Key to Button but else it is working the same. And it makes your code way nicer to read.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .