- Home /
,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();
}
}
}
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
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
It wasnt working at first though, till i changed your spelling of behavior to behaviour, because i am british and we spell it weirdly.
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?
hehe, my bad it must have this
else
{
if(previusSpeedRegistered)
{
rb.velocity=previusSpeed;
}
//else do nothing
}
Your answer
Follow this Question
Related Questions
The script is working but why when I try to change stuff in inspector the game is freezing ? 1 Answer
How to make ball sticky 1 Answer
How do I query the RigidbodyConstraints? 3 Answers
Android APK freezes during level load but not in Unity Editor 2 Answers
how to log for a build game freeze error 0 Answers