I’m trying to set up a bool identifying which direction on the X-axis a vehicle is moving (right or left). I figured TransformDirection is how I would go about doing this. Yet I get this error: "An object reference is required to access non-static member ‘UnityEngine.Transform.TransformDirection(UnityEngine.Vector3)’ ". Here is my script:
‘Transform’ is the class. You want the ‘transform’ which is the transform of the specific game object. Note there are some shortcuts. ‘transform.right’ is the same as transform.TransformDirection(Vector3.right). And for left, you can use ‘-transform.right’.
Your next problem is that Transform.TransformDirection() returns a Vector3, not a bool, and it does not return the direction you are heading. How will solve this problem will depend on how you are moving the object. If you object is facing in the direction of movement, you can use either Vector3.Dot() or Vector3.Angle() and compare Vector3.left and Vector3.right to figure out which direction it is going. If the object is not rotated, then you will have to calculate the velocity by subtracting the position of the previous frame. Checking the sign of the ‘x’ axis of the velocity will give you the direction the object is moving.