I know there are lots of scripts online detailing how to do implement a 2D magnet for infinite runners such as temple run or jetpack joyride, but I am not sure which one is the best. If anyone has a resource to the best practice, could you please reply back?
Thanks in advance.
EDIT: I have actually implemented this using the MoveTowards function in the Unity API. What I am looking for is having objects be attracted only if the player is within a detected range.
EDIT: Currently my script checks for range using Vector2.Distance, but for some reason the magnet stops working after a couple of seconds.
float step = speed * Time.deltaTime;
float distance = Vector2.Distance(coinTarget.position, gameObject.transform.position);
if (distance < 3 && Powerups.isMagnet)
transform.position = Vector3.MoveTowards(transform.position, coinTarget.position, step);