Hi there, I’m trying to use a hovering function I’ve made in another script on this object, when I try to use it, I get the error “An object reference is required for the non-static field, method or property”.
The function:
private Vector3 AbsoluteUp = new Vector3 (0f, 1f, 0f);
public void HoverFunction(Rigidbody ThisRB, Vector3 position,float HoverHeight, float Sensitivity, float MaxThrust)
{
Ray HeightMeasure = new Ray(position, -AbsoluteUp);
RaycastHit hit;
float mass = ThisRB.mass;
if (Physics.Raycast(HeightMeasure, out hit))
{
if (hit.distance < HoverHeight)
{
float HeightDelta = HoverHeight - hit.distance;
float HoverForce = (HeightDelta / (Mathf.Sqrt(Mathf.Pow(HoverHeight, 2) + Sensitivity * MaxThrust * mass)));
Vector3 HoverForceV3 = new Vector3(0, HoverForce, 0);
ThisRB.AddForceAtPosition(HoverForceV3, position);
}
}
}
Trying to use it:
Vector3 position = transform.position;
NewFunctionsTest.HoverFunction(rb, position, 5f, 3f, 15f);
Variable rb was defined as the current components rigidbody (which it has). Excluded for brevity. I’m quite new to Unity so feeling a bit lost!