like the title suggests how do I create an if statement that returns true if my gameObject is moving in the negative x axis.
If you’re talking about Physics velocity, then it’s pretty simple:
if (GetComponent().velocity.x < 0)
If you want to react to any kind of motion then it’s a bit more difficult because outside of the physics engine, there isn’t really “motion”, you just “teleport” things in small steps. It’s still possible though but you need to remember where the object was in the last frame to calculate its velocity:
Vector3 lastFramePosition; void Update() { var delta = transform.position - lastFramePosition; if (delta.x < 0) { // do something } lastFramePosition = transform.position; }
Here is a function you can call to check if you are moving along negative axis. Note this only works if you are using Key A | D or Left Arrow | Right Arrow
bool IsPositive () => Input.GetAxis("Horizontal") > 0;