- Home /
Magnet Power Up,moving the coins towards the player
I am making a 2D endless runner platformer sorta game where the player could collect Power Ups one of which is the Magnet,after collecting it the player has 5-15 seconds where all the coins that are overlaping need to be moved towards him the problem is i cannot get their transform position since the script is attached to the player game object and the overlaping is placing only the colliders in my collider2D array.I have managed to get the overlaping do its job and store only coins that are near the player but i am having difficulties moving them.When testing the i get the debug.logs i set up so everything is as is should be except the MoveTowards line i think it doesnt do anything.
`public Collider2D[] Colliders;
public LayerMask coinCheckLayerMask
if (StartMagnetTimer && !dead)
{
MagnetTimer += Time.deltaTime;
Vector2 pointA = (transform.position);
pointA.x -= 3;
pointA.y -= 3;
Vector2 pointB = (transform.position);
pointB.x += 3;
pointB.y += 3;
Colliders = Physics2D.OverlapAreaAll (pointA,pointB,coinCheckLayerMask);
if(Colliders.Length > 0)
{
Debug.Log ("Array is NOT empty");
foreach (var Collider in Colliders)
{
Vector2.MoveTowards (Collider.transform.position,transform.position,10.0f * Time.deltaTime);
Debug.Log ("Coin Moving Towards Player");
} '
Answer by Blaugrana · Jan 14, 2018 at 11:54 AM
Fixed it myself,for all the people having trouble making Vector2.MoveTowards work here is the right way to do it:
Collider.gameObject.transform.position = Vector2.MoveTowards(Collider.gameObject.transform.position,transform.position,10.0f * Time.deltaTime);
Your answer
Follow this Question
Related Questions
Endless Runner Coin Magnet Not working. 0 Answers
Magnetic object to make coins move to object 2 Answers
How to make different animations affect different parts of the object? 2 Answers
Using a transparent shader and I'm getting graphical glitches with textures overlapping each other 1 Answer
Overlapping sprites in Unity2D 1 Answer