2
\$\begingroup\$

I'm trying to simulate a time-slowing effect in Unity, which is supposed to slow down things like the physics engine, animator speeds etc. However, I also need certain GameObjects to be "immune" to this effect, so, as far as I understand, merely changing the value of Time.timeScale won't work.

The solution I've arrived at is to multiply certain values of non-immune rigidbodies by a factor (for example 0.1), and then divide by this factor when the slowdown effect ends to effectively reset them. I assumed doing this to the velocity and gravityScale of all non-immune rigidbodies, as well as the forces being applied to them, would be sufficient, but this does not seem to produce the desired effect.

As an example, jumping while time is slowed down produces a jump of 1/10th the height, that takes the same amount of time to complete (ie. reach the ground after jumping) when compared to jumping normally, when the desired effect is a jump of the same height, that takes 10 times longer to complete (assuming a slowdown factor of 0.1).

Any ideas on what other values must be changed in order to produce this effect? Or am I overthinking this and there is a simpler way to do it?

I'll gladly provide clarification/additional info if necessary. Thanks in advance.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Try setting different factor for velocity and gravityScale, where the factor of gravityScale is the square of the factor of velocity. like 0.1 and 0.01. \$\endgroup\$
    – Mangata
    Feb 14, 2023 at 4:12
  • \$\begingroup\$ @Mangata Ah, of course; gravity is acceleration, which is distance per time squared. Should have thought of that. Tried this out and it seems to work perfectly! Could you post your comment as an answer so I can mark as correct? \$\endgroup\$ Feb 14, 2023 at 4:36

1 Answer 1

2
\$\begingroup\$

For a general vertical upward throwing motion, the highest point is reached when the velocity is 0. We have:

$$ S=\frac{v^{2}}{2g},t=\frac{v}{g} $$

Let the factors of velocity and gravity be a and b respectively:

$$ v'=av,g'=bg $$

Now we need the height to be the same and the time to expand n times

$$ S'=\frac{v'^{2}}{2g'}=\frac{a^{2}v^{2}}{2bg}=S=\frac{v^{2}}{2g} $$ $$ t'=\frac{v'}{g'}=\frac{av}{bg}=n·t=n·\frac{v}{g} $$ So we get: $$ a^{2}=b $$ $$ a=n·b $$ And: $$ a=\frac{1}{n},b=\frac{1}{n^{2}}(a,b,n,g≠0) $$

C# code:

using UnityEngine;

public class UnscaledObject : MonoBehaviour
{
    private Rigidbody2D rigidbody;
    private float factorA =1f;
}
private void Start()
{
    rigidbody = GetComponent<Rigidbody2D>();
}
private void SetTimeScale(float n)
{
    factorA = 1/n;
    var factorB = factorA*factorA
    rigidbody.gravityScale = factorB;
}
private void SetVelocity(Vector2 v)
{
    rigidbody.velocity = factorA * v;
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Maybe add a last line to highlight the a to velocity and b to gravityScale with unity code lines to improve it even further \$\endgroup\$
    – Zibelas
    Feb 14, 2023 at 9:01

You must log in to answer this question.

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