• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SpeckledGecko · Sep 03, 2020 at 08:11 PM · freezefreezingfreezeposition

,Storing Vector3 arrays in another array?

I am new to Unity and c# and want to make a freeze effect for a projectile so you can freeze the object, where they would stop moving, and then unfreeze them. Freezing them is easy, as i can just make them kinematic, or just make their velocity 0(Which is what i did, as i turned off gravity) but making then unfreeze is a nightmare. I am trying to store their velocity in a seperate Vector3 array but it just wont work. Help!

 public class BulletPause : MonoBehaviour
     {
         Rigidbody rb;
         Vector3[] PreviousSpeed;
         void Start()
         {
             rb = GetComponent<Rigidbody>();    
         }
         void Update()
         {
             if(GameObject.Find("FirstPersonPlayer").GetComponent<PauseBehaviour>().IsPaused == true)
             {
                 int[] PreviousSpeed = new Vector3[rb.velocity.GetLength(0)];
                 rb.velocity.CopyTo(PreviousSpeed, 0);
                 rb.velocity = new Vector3(0, 0, 0);
             }
             if (GameObject.Find("FirstPersonPlayer").GetComponent<PauseBehaviour>().IsPaused != true)
             {
                 rb.velocity = PreviousSpeed();
             }
         }
     }
 
 

,I am very new to Unity and want to make a simple freeze mechanic for this object, so i can stop them freeze them, and then unfreeze with the same velocity i froze them with. Freezing them is easy, but un freezing is I found challenging, i tried storing their velocity before the freeze in a vector3 array but it wont copy over. Help!

 public class BulletPause : MonoBehaviour
 {
     Rigidbody rb;
     Vector3[] PreviousSpeed;
     void Start()
     {
         rb = GetComponent<Rigidbody>();    
     }
     void Update()
     {
         if(GameObject.Find("FirstPersonPlayer").GetComponent<PauseBehaviour>().IsPaused == true)
         {
             int[] PreviousSpeed = new Vector3[rb.velocity.GetLength(0)];
             rb.velocity.CopyTo(PreviousSpeed, 0);
             rb.velocity = new Vector3(0, 0, 0);
         }
         if (GameObject.Find("FirstPersonPlayer").GetComponent<PauseBehaviour>().IsPaused != true)
         {
             rb.velocity = PreviousSpeed();
         }
     }
 }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SpeckledGecko · Sep 03, 2020 at 08:15 PM 0
Share

the int[] previousSpeed bit on line 13 was just me failing to clean up after i changed some stuff to try and make it work, it didnt work when that was Vector3 either

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AdrianTEC · Sep 04, 2020 at 05:04 PM

Some advice, avoid at all costs using "Find()" in update() its a very expensive Function

you can do it in start()

 public GameObject  player;
 private PauseBehavior pause;
 void Start()
    {
          //you can drop the player in inspector window
         // can also reference it here
                    player=GameObject.Find("FirstPersonPlayer");
                    pause= player.GetComponent<PauseBehavior>();
              
    }


Now, I don't understand why you want to store your velocity in arrays, I made it storing the previous speed in a local variable, I tried this based in your code

 void Update()
     {
             if(pause.Ispaused) //you cand omit ==true  if(true)
                 {
                     if(!previusSpeedRegistered)  //you must have a boolean for only register the previus speed  only one time
                         {
                             previusSpeed= rb.velocity;
                             previusSpeedRegistered=true;
                         }
 
                     rb.velocity= new Vector3(0,0,0);
 
                 }
             else
                 {
                     rb.velocity=previusSpeed;
                 }
     }


Tell me if this works xd

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SpeckledGecko · Sep 04, 2020 at 06:59 PM 0
Share

thanks man!

avatar image SpeckledGecko · Sep 04, 2020 at 07:15 PM 0
Share

It wasnt working at first though, till i changed your spelling of behavior to behaviour, because i am british and we spell it weirdly.

avatar image SpeckledGecko · Sep 04, 2020 at 07:35 PM 0
Share

ive run into another problem now. When IfPaused is not true it will change the velocity to previousSpeed, but it also hasnt assigned previous speed anything because it only does that when IfPaused is true so it acts like previousSpeed is (0,0,0) and will stop all velocity. Any ideas to combat this?

avatar image AdrianTEC · Sep 04, 2020 at 08:32 PM 0
Share

hehe, my bad it must have this

              else
                  {
                      if(previusSpeedRegistered)
                          {
                                          rb.velocity=previusSpeed;
                         }
                                     //else do nothing
 
                  }
avatar image SpeckledGecko · Sep 05, 2020 at 09:40 AM 0
Share

Thanks, now it works perfectly

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity was freezing, and now it won't open because of this error. 3 Answers

How do you REALLY freeze a Rigid Body (no dragging allowed)? 1 Answer

How do I “freeze” an object after a collision? 3 Answers

Unity freezes temporarily when recompiling/refreshing scripts 2 Answers

Unity freezes after adding a new script 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges