# Coin magnet : speed of attraction problem

Hi,

I’ve created a very basic test scene so I can script my coin magnet power up, at the moment I’m using OverlapBox to detect any nearby coins that are within my ‘coinslayer, and if there is, then move these coins towards my player, however, the coins are instantly being moved to the players position, changing the magnetAttractSpeed to super small values doesn’t seem to affect it either, I’m obviously doing something really stupid, was hoping someone might point me in the right direction ?

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerMotor : MonoBehaviour {

public GameObject character;
private float playerSpeed = 8.0f;

public Vector3 magnetRange = new Vector3(2.0f, 2.0f, 2.0f);
public float magnetAttractSpeed = 10f;
public float magnetTimeLimit = 15f;
public Collider[] coinsFound;

void Start () {
}

void Update () {

if (Input.GetKey(KeyCode.RightArrow)) {
transform.position += Vector3.right * playerSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.position += Vector3.left* playerSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow)) {
transform.position += Vector3.forward * playerSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow)) {
transform.position += Vector3.back* playerSpeed * Time.deltaTime;
}

//        if (hit.point.z > transform.position.z && hit.gameObject.tag == "magnet") {
//            attractCoins ();
//        }

attractCoins ();
}

// Magnet function - attractCoins - needs timer amongest other things
void attractCoins () {
// Find colliders within magnetRange and in layer mask
coinsFound = Physics.OverlapBox(transform.position, magnetRange, Quaternion.identity, mask);

foreach (Collider coinsCol in coinsFound) {
// Move coins towards player
coinsCol.transform.position = Vector3.MoveTowards(transform.position, character.transform.position, magnetAttractSpeed * Time.deltaTime);
}
}

void OnDrawGizmosSelected() {
Gizmos.color = new Color (1, 0, 0, 0.25f);
Gizmos.DrawCube (transform.position, magnetRange * 2);
}
}
``````

Yeah. Your MoveTowards is moving them from your playerMotor’s transform towards the player’s transform. Those are probably the same thing, and definitely not what you want:

``````// Move coins towards player
coinsCol.transform.position = Vector3.MoveTowards(transform.position, character.transform.position, magnetAttractSpeed * Time.deltaTime);
``````

Should be:

``````// Move coins towards player
coinsCol.transform.position = Vector3.MoveTowards(coinsCol.transform.position, character.transform.position, magnetAttractSpeed * Time.deltaTime);
``````
1 Like

Perfect @Baste

Thanks for the help, knew it was going to be something stupid at my end !

Regards.