- Home /
Question by
yusufgunay · Sep 16, 2020 at 07:59 PM ·
instantiateaddforceslowmotion
Addforce function for an Instantiated object is not working after slow motion effect!
I have a code block like below,
Basically I am trying to instantiate an object(asteroid) and add force to it. Everything works very fine. But in another script I detect the collision and I gave slow motion effect for four seconds. But after slow motion effect happens, the objects are still instantiated yet the addforce is not working anymore. The objects holds on air.
I really dont know what is the problem!
Instantiate code++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public Rigidbody asOne;
public float AsShootPower = 50f;
public Transform AsteroidPos;
Vector3 offset;
public float astCount = 100f;
void Start()
{
Invoke("StartRain", 2);
}
private void StartRain()
{
StartCoroutine(countDown());
}
IEnumerator countDown()
{
while (astCount > 0)
{
offset = new Vector3(Random.Range(-15,15), Random.Range(-1, 1), Random.Range(-15, 15));
Vector3 randomPosition = AsteroidPos.transform.position + offset;
Rigidbody newAst;
newAst = Instantiate(asOne, randomPosition, AsteroidPos.rotation);
newAst.GetComponent<Rigidbody>().AddForce(AsteroidPos.forward * -AsShootPower);
yield return new WaitForSeconds(Random.Range(0,2));
astCount--;
}
}
slowmo code +++++++++++++++++++++++++++++++
public float SlowdownFactor = 0.05f;
public float SlowdownLength = 4f;
// Update is called once per frame
void Update()
{
Time.timeScale += (1f / SlowdownLength) * Time.unscaledDeltaTime;
Time.timeScale = Mathf.Clamp(Time.timeScale, 0f, 1f);
}
public void DoSLowMotion()
{
Time.timeScale = SlowdownFactor;
Time.fixedDeltaTime = Time.timeScale * .02f;
}
Slowmo Detector++++++++++++++
public TimeManager timeManager;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Asteroid")
{
Debug.Log("Asteroid is coming!!");
timeManager.DoSLowMotion();
}
}
Comment
Your answer