In my game, I have a function called every frame like the following
public static void Vehicle_Idle_DownHit(GameGod gameGod, CarControl vehicleAI)
{
RaycastHit hit;
Vector3 startPos = new Vector3(vehicleAI.Mytransform.position.x + 1, vehicleAI.Mytransform.position.y + 1, vehicleAI.Mytransform.position.z);
//Debug.DrawRay(startPos, vehicleAI.Mytransform.TransformDirection(Vector3.down), Color.red);
switch (Physics.Raycast(startPos, vehicleAI.Mytransform.TransformDirection(Vector3.down), out hit, 2, gameGod.Layers[0]))
{
case true:
vehicleAI.Mytransform.rotation = Quaternion.LookRotation(Vector3.Cross(vehicleAI.Mytransform.TransformDirection(Vector3.right), hit.normal));
vehicleAI.Mytransform.position = new Vector3(vehicleAI.Mytransform.position.x, hit.point.y, vehicleAI.Mytransform.position.z);
}
}
Having seen that the raycastHit struct is quite complex (it has a collider, an rb, etc.) Having to call it on 10 different objects at the same time, to optimize performances it is better for me to create a raycastHit variable for each of these objects, and use that as a reference , or can I create it at runtime (like in the example)?