I am moving a block back and forth along a single axis, it follows its target only while it is between two points.
public GameObject Target;
public float speed = 30f;
public bool lockX = false;
public bool lockY = false;
public bool lockZ = false;
public float bounds = 20f;
void FixedUpdate () {
Vector3 direction = (Target.transform.position - transform.position).normalized;
direction.x = Mathf.Clamp(direction.x, -bounds, bounds);
direction.y = Mathf.Clamp(direction.y, -bounds, bounds);
direction.z = Mathf.Clamp(direction.z, -bounds, bounds);
if (lockX)
{
direction.x = 0;
}
if (lockY)
{
direction.y = 0;
}
if (lockZ)
{
direction.z = 0;
}
rigidbody.MovePosition(transform.position + direction * speed * Time.deltaTime);
}
What I want it to do is if it gets to the bounds, stop following in that direction. I have tried clamping to the bounds in each direction but the blocks keep following. I am pretty sure I need to clamp it some other way but not sure what the best way would be.
That fixed it! Thank you so much.
– SpectralEdge