Code performance. How to write faster LookRotation for many Transforms?

This code works. I use it to point GamObject to a target, the target is placed on the screen by theplane.Raycast. Everything works.

But I’m not a fan of duplicate code and I don’t like duplicate code, so I changed my code to get an array of Objects to aim and point them to target since everything is fired from the same Gameobject. I was wondering if it was a performer or some other method or trick would do it.

Here is the code.

→ The part I am interested in is where I get all Objects (aimAtTarget) and aim them at the Target the For loop part.

using UnityEngine;
using UnityEngine.InputSystem;

public class HandAim : MonoBehaviour
{
    private Vector3 screenPosition;
    private Vector3 worldPosition;
    Plane plane = new Plane(Vector3.forward, 0);

    [SerializeField] Transform target;// target used for aiming at.
    [SerializeField] Transform[] aimAtTarget;// Object to aim at the target

    [SerializeField] float damping = 2f;
    int aimAtTargetLenght;
    
    void Awake()
    {
        aimAtTargetLenght = aimAtTarget.Length;
    }

    void Update()
    {
        // Position the aim target at world point

        screenPosition = Mouse.current.position.ReadValue();
        Ray ray = Camera.main.ScreenPointToRay(screenPosition);

        if (plane.Raycast(ray, out float distance))
        {
            worldPosition = ray.GetPoint(distance);
        }
        for (int i = 0; i < aimAtTargetLenght; i++)
        {
            var lookPos = target.position - aimAtTarget[i].position;
            lookPos.z = 0;

            var rotation = Quaternion.LookRotation(lookPos);
            aimAtTarget[i].rotation = Quaternion.Slerp(aimAtTarget[i].rotation, rotation, Time.deltaTime * damping);
            target.position = worldPosition;
        }
    }

}

TransformAccessArray & IJobParallelForTransform duo was designed for cases where you need to move or rotate A LOT of Transforms. Performance gains are a results of multithreaded Burst-compiled IJobParallelForTransform jobs here.

AimController.cs

using UnityEngine;
using UnityEngine.Jobs;
using Unity.Jobs;
using Unity.Collections;
using UnityEngine.InputSystem;

public class AimController : MonoBehaviour
{

    [SerializeField] Camera _camera;
    [SerializeField] Transform _target;
    [SerializeField] Transform[] _aimAtTarget;
    [SerializeField] float _rotationStepMultiplier = 2f;
    [SerializeField] TransformAccessArray _transformAccessArray;
    JobHandle Dependency;

    void OnEnable ()
    {
        _transformAccessArray = new TransformAccessArray( _aimAtTarget );
    }

    void OnDisable ()
    {
        if( _transformAccessArray.isCreated ) _transformAccessArray.Dispose();
    }

    void Update ()
    {
        var pointer = Pointer.current;
        if( pointer!=null )
        {
            Vector2 pointerPos = pointer.position.value;
            var plane = new Plane( inNormal:Vector3.forward , inPoint:Vector3.zero );
            Ray ray = _camera.ScreenPointToRay(pointerPos);
            if( plane.Raycast(ray,out float hitDistance) )
                _target.position = ray.origin + ray.direction*hitDistance;
        }
        
        Dependency.Complete();

        var job = new LookAtTargetJob
        {
            target = _target.position ,
            maxStep01 = Time.deltaTime * _rotationStepMultiplier ,
        };
        Dependency = job.Schedule( _transformAccessArray , Dependency );
    }

    [Unity.Burst.BurstCompile]
    public struct LookAtTargetJob : IJobParallelForTransform
    {

	    public Vector3 target;
        public float maxStep01;

	    void IJobParallelForTransform.Execute ( int index , TransformAccess transform )
		{
            Vector3 position = transform.position; 
            Vector3 lookDirectionXY = new Vector3(target.x,target.y) - new Vector3(position.x,position.y);
            transform.rotation = Quaternion.Slerp(
                transform.rotation ,
                Quaternion.LookRotation( lookDirectionXY ) ,
                maxStep01
            );
        }

    }

}