Slow down 1 script instead of all?

So i´m trying to create a power-up which on pickup tells another script “The movement script” that it should slow down.

The script that it should slow down looks like this:

function FixedUpdate ()
{
       
        if (Input.GetKey (KeyCode.W) && jumpCount == 0)
        {
            GetComponent.<Rigidbody>().AddForce (Vector3.forward * 15);
        }
        if (Input.GetKey (KeyCode.S))
        {
            GetComponent.<Rigidbody>().AddForce (Vector3.back * 10);
        }
        if (Input.GetKey (KeyCode.A))
        {
            GetComponent.<Rigidbody>().AddForce (Vector3.left * 14);
        }
        if (Input.GetKey (KeyCode.D))
        {
            GetComponent.<Rigidbody>().AddForce (Vector3.right * 14);
        }
        if (Input.GetKey (KeyCode.Space) && jumpCount == 0)
        {
            GetComponent.<Rigidbody>().AddForce(Vector3.up * jumpHeight, ForceMode.Impulse); jumpCount = 1;
        }   
        {
            GetComponent.<Rigidbody>().velocity = Vector3.ClampMagnitude(GetComponent.<Rigidbody>().velocity, maxVel);
        }  
       
        if (Slow == true)
        {
            Time.timeScale = 0.5;
              Time.fixedDeltaTime = 0.02 * Time.timeScale;
        }
}

BUT

For some reason it slows down everything in the entire scene and not only the objects in the script, why?

I want everything related to player 1 to be slowed EXCEPT the Timer script, how?

Yes changing Time.timeScale will slow down the entire project. You can use Time.unscaledTime to reference your other scripts and they will be un-affected by any change made in Time.timeScale. You can also use Time.unscaledDeltaTime too.

You totally misunderstood this sorry.

I have a player1 movement script, a player 2 movement script and the share a timer.

I want it so when player 1 hits the “slow down” object Player 2s movement script will be slowed down but not the timer and then the opposite when player 2 hits the ball

Are you looking for something like the stasis effect in Dead Space?

I´ve actually never played Dead Space so i cant tell.
But it´s a very common thing that i´m trying to achieve.

I have a Split-screen game with player1 and player 2.

What i want to happen

Player 1 pickup / collide with power-up ball = Player2 time is slowed by 50% but not his timer!
So basicly only his movement script :slight_smile: ?

Well, there’s only one flow of time in Unity, and as far as I know you can’t modify individual Rigidbodies sampling of said time. You can however modify the forces you apply to rigidbodies, but you will need to modify them all yourself, including gravity and drag. Try just multiplying your forces with 0.5f when slowed, though not sure how accurate the simulation would be.

Ye i tried to multiply my Addforce by 0,5 but then it takes too long for it to get up in a good speed so it cant get enough speed for the different jumps, thanks for your help tho - now i understand time better :slight_smile:

I don’t think I mis-understood. Create a timer using ‘unscaled’ time. That way it will be unaffected by any changes you make in Time.timescale. Also you’ll want to cache your Rigidbody rather than using GetComponent over and over. You would also probably be better off using a Event rather than all the if conditionals.

Yeah you didn´t mis-understand me, it was just me who didnt make sense at all :3

Most of my scripts have A LOT If statements - I´ve never heard of Event, could you explain a bit :)?

Try this guy out. It will give you a nice GUI in the inspector. From that GUI you can call functions (events) to any other script in your project. You probably still need the if conditionals but this will be much more flexible as you can send events from one place to anywhere in your project.

import UnityEngine.Events;

//these are your events that will be added to the inspector.  Once you see them hit the "plus sign" add the object you want to send an event to and connect it with the function you want to call.
var thisIsMyFirstEvent: UnityEvent;
var thisIsSecondEvent: UnityEvent;

function Update(){ if (Input.GetKey(KeyCode.W)) thisIsMyFirstEvent.Invoke(); } //this invoke is how you call your event