I have a coroutine which I'm trying to make a dash script from (using the unity New Input System & standard unity CharacterController) and calling the Coroutine in the scriptable object makes unity seize up.
If I were to put the controller.Move() in Activate() it would work but blink instead of dash. I've tried to use a coroutine to avoid this, but I can't use coroutines in ScriptableObjects so instead I made a blank MonoBehaviour called MonoInstance (which you can see below). I use this solely to call the StartCoroutine function from.
This crashes Unity, now my head hurts. Help
Tl;Dr - Unity crash. How make not crash. Brain hurt.
Ability
public class Ability : ScriptableObject
{
public new string name;
public float cooldownTime;
public float activeTime;
public virtual void Activate(GameObject parent) { }
}
DashAbility
[CreateAssetMenu]
public class DashAbility : Ability
{
private PlayerController movement;
private CharacterController controller;
public float dashVelocity;
public override void Activate(GameObject parent)
{
movement = parent.GetComponent<PlayerController>();
controller = parent.GetComponent<CharacterController>();
MonoInstance.instance.StartCoroutine(Dashing());
}
IEnumerator Dashing()
{
float startTime = Time.time;
while(Time.time < startTime activeTime)
{
controller.Move(movement.move.normalized * dashVelocity * Time.deltaTime);
}
yield return null;
}
}
MonoInstance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonoInstance : MonoBehaviour
{
public static MonoInstance instance;
private void Start()
{
MonoInstance.instance = this;
}
}
CodePudding user response:
I hate unity, fix & explanation down below:
DashAbility
IEnumerator Dashing()
{
float startTime = Time.time;
while (Time.time < startTime activeTime)
{
controller.Move(movement.move.normalized * dashVelocity * Time.deltaTime);
yield return new WaitForSeconds(0.01f);
}
yield return null;
}
Unity tried to do too much at once, and called the time while loop thousands of times per millisecond where the character controller (probably) wouldn't even have moved yet (because Time.deltaTime was 0, since the time difference in frames is nothing). This made unity cry. This made me cry.
Adding a WaitForSeconds(?.??f) made unity not do so much, and now it doesn't crash.
Tl;dr - Told unity to take a chill pill and it stopped seizing up
CodePudding user response:
In fact, your code should be written this way. Because 0.01 seconds does not show the duration of 1 frame.
IEnumerator Dashing()
{
var startTime = Time.time;
while (Time.time < startTime activeTime)
{
controller.Move(movement.move.normalized * dashVelocity * Time.deltaTime);
yield return new WaitForEndOfFrame();
}
}