I have created a magnet script but there is only 1 problem left.
First of all here is the script:
using UnityEngine;
using System.Collections;
public class Coin : MonoBehaviour {
private GameObject target;
void Update() {
if(target) {
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, 3.0f * Time.deltaTime);
}
//I know it doesn't work. lol. It's only an example
/*if (transform.position = target.transform.position) {
target = null;
}*/
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Magnet") {
target = col.gameObject;
}
}
}
The problem is I want that if the “Coin” reaches the target position, target should be then = null.
I have tried it with WaitForSeconds() but it’s not good. I don’t know how I should do that.
Setup a new Layer in Layer manager Call it whatever you want it
Mine looked like “User Layer 8 = Player” . Note the int value after User Layer " 8 "
In the Ontrigger event set your int value replacing " x "
Physics2D.IgnoreLayerCollision(0, x);
Coin.cs:
using UnityEngine;
using System.Collections;
public class Coin : MonoBehaviour {
public GameObject target;
void Update() {
if (target != null) {
transform.position = Vector3.MoveTowards (transform.position, target.transform.position, 3.0f * Time.deltaTime);
if (Vector3.Distance (transform.position, target.transform.position) <= 0) {
Debug.Log ("Entered");
gameObject.SetActive (false);
target = null;
}
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Magnet") {
target = col.gameObject;
Physics2D.IgnoreLayerCollision(0,8);
}
}
}
I’m trying to figure out your expected outcome. Can we get a better understanding of the magnet script for the coin?
Do you want the coin to be attracted to the player like the million other runner phone games i.e. temple run?
If yes then the magnet script should have the coin Lerp or “MoveTowards” to the player until it is hit by the player then it should disappear??
Unless what you want (which now that i read it some more makes more sense) a magnet to attract coins and hold them? would you want to move the magnet and still have them hold on?
Basically you could use a distance measurement between two positions and stop once within a certain distance. dont use 0 or magnet.position = coin.position. thats almost impossible…well kind of. you could just make coin.position = magnet.position once it was close enough.
try this function, i use for moving platforms from one location to another, it should work for you.
// transform.position is COIN, _currentPoint.position would be magnet.position
var distanceSquard = (transform.position - _currentPoint.position).sqrMagnitude;
//MaxDistanceToGoal is how close you need to be to have reached your goal. you can call it something else. As this was for a moving platform.
if(distanceSquard < MaxDistanceToGoal * MaxDistanceToGoal)
//do what you want once its close enough
I give you this code because it appears that you are working in 2D mode and this is a basic distance calculation in 2D.