UNT0036 Inefficient method to get position and rotation

October 19, 2023 ยท View on GitHub

Accessing the Transform/TransformAccess should be done as few times as possible for performance reasons. Instead of reading position and rotation sequentially, you should use GetPositionAndRotation() method.

Examples of patterns that are flagged by this analyzer

using UnityEngine;

class Camera : MonoBehaviour
{
    void Update()
    {
        var position = transform.position;
        var rotation = transform.rotation;
    }
}

Solution

Fix assignment:

using UnityEngine;

class Camera : MonoBehaviour
{
    void Update()
    {
        transform.GetPositionAndRotation(out var position, out var rotation);
    }
}

A code fix is offered for this diagnostic to automatically apply this change.