Skip to content
English
  • There are no suggestions because the search field is empty.

Helper Functions

Learn how to simplify your automated test through the use of Helper Functions

Sometimes it is necessary to perform test/client-side calculations that can be tricky but are required for testing. That's why we have compiled a few that have proven useful over the years below. These can be added to your tests directly or stored in a separate class and used directly in your test code.

Quaternion RotateTo(string objectToRotate, string targetObject, Vector3 offset = null, Vector3 orientation = null)
{
//GameObject player = GameObject.Find(objectToRotate);
Vector3 playerPosition = api.GetObjectPosition($"//*[@name='{objectToRotate}']");
Vector3 targetPosition = api.GetObjectPosition($"//*[@name='{targetObject}']");
if (offset != null)
{
targetPosition = Vec3Addition(targetPosition, offset);
}

Vector3 targetVector = Vec3Subtraction(targetPosition, playerPosition);
targetVector = NormalizeVector(targetVector);

if (orientation == null)
orientation = new Vector3(0.0f, 1.0f, 0.0f);

Quaternion rotation = api.CallMethod<Quaternion>("fn:type('UnityEngine.Quaternion')", "LookRotation", new object[] { targetVector, orientation });
return rotation;
}

public Vector3 Vec3Addition(Vector3 vecA, Vector3 vecB)
{
return new Vector3(vecA.x + vecB.x, vecA.y + vecB.y, vecA.z + vecB.z);
}

public Vector3 Vec3Subtraction(Vector3 vecA, Vector3 vecB)
{
return new Vector3(vecA.x - vecB.x, vecA.y - vecB.y, vecA.z - vecB.z);
}

public Vector3 Vec3Multiplication(Vector3 vec, float multiplier)
{
return new Vector3(vec.x * multiplier, vec.y * multiplier, vec.z * multiplier);
}

public float SquareMagnitude(Vector3 vec)
{
// Square each vector value and add them
return ((vec.x * vec.x) + (vec.y * vec.y) + (vec.z * vec.z));
}

public Vector3 NormalizeVector(Vector3 vecToNormalize)
{
// Find the magnitude squared, without the square root
float squareMag = SquareMagnitude(vecToNormalize);

// Do the fast inverse square root
double fastInvSqrt = 1 / Math.Sqrt(squareMag);

// Multiply values to get the normalized vector
Vector3 normVector = Vec3Multiplication(vecToNormalize, (float)fastInvSqrt);
return normVector;
}

Do you have a useful helper method you would like to share? Let us know on Slack, or by emailing support@gamedriver.io