I’m trying to have both a freeze time and turn gravity off mechanic where I can shoot something off without gravity, freeze it, and then unfreeze it and have it still moving but when I try to freeze the rigid body and save the velocity it doesn’t apply it later. my code:
using UnityEngine;
using System.Collections;
public class ObjectPhysics : MonoBehaviour {
Vector3 savedVelocity;
Vector3 savedAngularVelocity;
private Rigidbody rb;
public ObjectChanger script;
GameObject target;
void Start () {
rb = GetComponent<Rigidbody>();
target = GameObject.FindGameObjectWithTag("Player");
script = target.GetComponent<ObjectChanger>();
}
// Update is called once per frame
void Update () {
if(script.time == false){
savedVelocity = rb.velocity;
savedAngularVelocity = rb.angularVelocity;
rb.isKinematic = true;
}
else{
rb.isKinematic = false;
rb.WakeUp();
rb.AddForce(savedVelocity);
rb.AddTorque(savedAngularVelocity);
}
if(script.gravityAndFriction == false){
rb.useGravity = false;
rb.angularDrag = 0;
rb.drag = 0;
}
if(script.gravityAndFriction == true){
rb.useGravity = true;
rb.angularDrag = 0.05f;
rb.drag = 0;
}
}
}