- Home /
Disable rigidbody until a action is performed.
I know there are a couple of posts (Sort of) like this but I could not figure it out. So I have a 3d pc game and I have the AI but all it does is face the player. I want it to fly towards the player using a rigidbody but before it flies towards the player I want it to just float in the air for 5 seconds with no rigidbody when it is cloned and then enable the rigidbody. In other words, I just want my rigidbody to toggle off for 5 seconds after it spawns and then it will turn back on. If anyone could assist me in scripting that would be great because I tried nothing so far, thanks!
Answer by Tyrdle · Oct 01, 2019 at 01:29 AM
I'd suggest having your enemy prefab (assuming you are using one) to start with the rigidbody off, just untick the rigidbody component on the prefab. Depending on how you spawn it, whether it creates the object or just moves it to the spawn location, you will want to use a Coroutine https://docs.unity3d.com/ScriptReference/Coroutine.html or you could simply just add Time.deltaTime to a float and check once it's > 5 but thats not entirely efficient, it's easier though.
Whoops I forget to mention how to then enable it, however you wait the 5 seconds, after that use GetComponent<Rigidbody>().isActive = true;
I believe.
Thanks for helping but GetComponent().isActive = true; sends me isActive is not a thing. But when I debug.log it works.
Answer by zacharif · Oct 02, 2019 at 12:37 AM
Ok I found it out for ay future viewers, assign a public rigidbody and write rb.isKinematic = false; to toggle on and rb.isKinematic = true; to toggle it off, code looks like this but tyrdle helped a lot using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shoot : MonoBehaviour { public Rigidbody rb; // Start is called before the first frame update void Start() { StartCoroutine(Example()); } IEnumerator Example() { rb.isKinematic = true; yield return new WaitForSeconds(5); //change this for how much you want it to wait for rb.isKinematic = false; } }