This week on “Things that Unity used to be fine with but now no longer likes” we take a look at the script I use to control ALL of the doors in my game, none of which work now.
Each time I call this script (ask it to open, close or toggle any doors or anything) I get 1 unique error per call, and 1 other error for each object I am trying to adjust.
The Unique Error
Assertion failed on expression: 'CompareApproximately(SqrMagnitude(result), 1.0F)'
UnityEngine.Transform:set_localEulerAngles(Vector3)
<DoMovement>c__Iterator0:MoveNext() (at Assets/Scripts/General Interaction/Door/DoorGeneric.cs:81)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
The Per-Object Error
transform.localRotation assign attempt for 'INSERTOBJECTNAMEHERE' is not valid. Input rotation is { NaN, NaN, NaN, NaN }.
UnityEngine.Transform:set_localEulerAngles(Vector3)
<DoMovement>c__Iterator0:MoveNext() (at Assets/Scripts/General Interaction/Door/DoorGeneric.cs:81)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
The Script
using UnityEngine;
using UnityEngine.Events;
#if UNITY_EDITOR
//using UnityEditor;
#endif
using System.Collections;
[System.Serializable]
public class Door
{
public GameObject door;
public float time = 1.0f;
public bool rotation;
public Vector3 open;
public Vector3 closed;
public int delaySeconds;
public AudioClip clip;
public UnityEvent after;
}
public class DoorGeneric : MonoBehaviour {
public Door[] doors;
public bool moving;
public bool completedPos;
public bool completedRot;
public bool closed = true;
public void ToggleDoor()
{
if(!moving && closed)
{
OpenDoor();
}
if(!moving && !closed)
{
CloseDoor();
}
}
public void OpenDoor()
{
if (!moving && closed)
{
foreach(Door door in doors)
{
StartCoroutine(DoMovement(door.closed, door.open, door.time, door.door, door.rotation, door.delaySeconds, door.after));
door.door.GetComponent<AudioSource>().PlayOneShot(door.clip);
}
closed = false;
}
}
public void CloseDoor()
{
if (!moving && !closed)
{
foreach (Door door in doors)
{
StartCoroutine(DoMovement(door.open, door.closed, door.time, door.door, door.rotation, door.delaySeconds, door.after));
door.door.GetComponent<AudioSource>().PlayOneShot(door.clip);
}
closed = true;
}
}
IEnumerator DoMovement(Vector3 startPosition, Vector3 endPosition, float moveTime, GameObject obj, bool rotation, int delay, UnityEvent after)
{
moving = true;
yield return new WaitForSeconds(delay);
float startTime = Time.time;
float endTime = startTime + moveTime;
do
{
float timeProgressedPosition = (Time.time - startTime) / moveTime;
timeProgressedPosition = Mathf.Sin(timeProgressedPosition * Mathf.PI * 0.5f);
timeProgressedPosition = timeProgressedPosition * timeProgressedPosition * timeProgressedPosition * (timeProgressedPosition * (6f * timeProgressedPosition - 15f) + 10f);
if (rotation)
{
obj.transform.localEulerAngles = Vector3.Lerp(startPosition, endPosition, timeProgressedPosition);
}
else
{
obj.transform.localPosition = Vector3.Lerp(startPosition, endPosition, timeProgressedPosition);
}
yield return new WaitForFixedUpdate();
}
while (Time.time < endTime);
after.Invoke();
moving = false;
}
public bool isMoving()
{
return moving;
}
}
Any ideas as to why since Unity 4.8, up to 5.5 it was fine, ans as soon as I update to 5.6, this starts happening… or just how to fix it?