Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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.

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

how to log for a build game freeze error 0 Answers

Android APK freezes during level load but not in Unity Editor 2 Answers

AssetBundle.LoadAssetAsync case freezing in Unity5.5.0p1 5 Answers

problems with ReplacePrefab script 0 Answers

Unity crashes trying to get pixels and using Debug.Log 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges