Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How To Change Vector2 Velocity while storing it's Original State?

Discussion in 'Scripting' started by dollisonrayanthony, Dec 27, 2022.

  1. dollisonrayanthony

    dollisonrayanthony

    Joined:
    Mar 25, 2020
    Posts:
    4
    I'm currently implementing a pause-like mechanic , where-in if you press space then it stops and once pressed again it resumes, but my problem is resuming the objects Velocity, I've tried
    Code (CSharp):
    1. OrgVeloc = rb.Velocity;
    2. rb.Velocity = Vector2.zero;  
    then when it "resumes":
    Code (CSharp):
    1. rb.Velocity = OrgVeloc;
    but the problem is that I the rb.Velocity is still Zero. I think this is because OrgVeloc is just a pointer and so changes to rb.Velocity also changes itself, any ideas how I can FullCopy the Velocity
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    That's not the case. Vector3 and Vector2 are structs which means that they are always copied just like integers and floats (unlike classes in C#). Your problem is somewhere else.

    I agree with tughanalpaslan suggestion, that it will be a lot easier if you just used timeScale for pausing physics.
     
    orionsyndrome likes this.
  3. tughanalpaslan

    tughanalpaslan

    Joined:
    Jul 25, 2020
    Posts:
    50
    Agggh I deleted the comment because I believed it may not be the solution and there may be other ways to achieve this at the same time you posted :)

    So here is my suggestion again, you can set the Time.timeScale to 0 to stop the time in the game. Later, make sure that you set it to 1 so that time flows as usual. You can also assign float values such as .5f slow down the time by half.

    Edit: Additionally, you can also use Time.unscaledTime for certain objects which will help you to intervene time independent from timeScale, i.e., the time in your game.
     
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    You can never stop a fixed update. It defaults at the lowest value if passing zero into it.
    If you are slowing down time in general you open up issues with the frame rate of options menus and the like.
    You actually have to destroy all rigidbodies and recreate them based on a memory of what they were. Because there is no means to enable or disable them or stop the physics fixed update time from calculating the physics.
    well actually depending how your rigidbodies are set up you might be able to use isKinematic to cancel physics and test enable physics, or if it was 2D you may ask if it is simulated.
    it’s choppy you can get it to work like this but I prefer to write my own physics systems as they are more suitable in granting greater control such as time stop and time freezing for the objects.
     
  5. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    If you do not want to change the timescale, then you can, indeed, do what you proposed.

    I tried implementing it real quick, and it's behaving correctly. The only hang-up: make sure you set isKinematic to false before setting the velocity. If you set the velocity first, then it'll get reset to zero when you make the object non-kinematic!

    (i noticed that you weren't setting that in your example -- making the Rigidbody "kinematic" means that it's no longer affected by forces, which will freeze it in place until you make it non-kinematic again)

    I haven't had too much trouble with timescale and menus. You just need to make sure you use Time.unscaledDeltaTime instead of Time.deltaTime for things like transitions.
     
    Last edited: Dec 27, 2022
  6. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
  7. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    are you setting the velocity in Update or FixedUpdate? If its not in FixedUpdate, that could be the reason, as the physics thread may just overwrite the value after you set it.