I have an object which sometimes can move away from my original position, how can I first check that he has moved away and then return him to my position, I just can’t understand
Save it’s position to variable in Start method, next in update set it back from the variable.
Simple example:
Vector3 startPosition;
void Start()
{
startPosition = transform.position;
}
void Update()
{
// if you need check distance, you can do it like this:
if ((transform.position - startPosition).magnitude > 0.5f)
transform.position = startPosition;
}
It will return object to start position, if it moved more than 0.5 in any direction.
When you subtract one position from another, you get direction vector. Magnitude returns it length, so we can get distance between two positions this way.