Question by
muhndalobaidi · Mar 29, 2020 at 10:21 AM ·
transform.positioncoinsmagnet
magnet power up in 3d endless runner game?
I followed a toturial to make a magnet for my endless runner project, where I used a wide collider on the player to detect collision for coin and pooling them.
so for the magnet i used this code
public class MagnetPowerUp : MonoBehaviour
{
public GameObject coinDetector;
void Start()
{
coinDetector = GameObject.FindGameObjectWithTag("coinDetector");
coinDetector.SetActive(false);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
StartCoroutine(ActivateCoin());
Destroy(gameObject);
}
}
IEnumerator ActivateCoin()
{
coinDetector.SetActive(true);
yield return new WaitForSeconds(10f);
coinDetector.SetActive(false);
}
}
and for the coin I used this movingcoin code
public class CoinMove : MonoBehaviour
{
coinMagnet coinScript;
// Start is called before the first frame update
void Start()
{
coinScript = gameObject.GetComponent<coinMagnet>();
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, coinScript.playerTransform.position, coinScript.moveSpeed * Time.deltaTime);
}
}
for Activating the moving code I used this code
public class coinMagnet : MonoBehaviour
{
public Transform playerTransform;
public float moveSpeed = 17f;
CoinMove coinMoveScript;
void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
coinMoveScript = gameObject.GetComponent<CoinMove>();
}
void OnTriggerEnter(Collider magnetCollider)
{
if (magnetCollider.gameObject.tag == "coinDetector")
{
coinMoveScript.enabled = true;
}
}
}
so when I start the game the coins come directly to the player without even pick up the magnet, the coins start to rush to the player
Sooo what am I doing wrong?
Comment
Answer by tormentoarmagedoom · Mar 29, 2020 at 01:58 PM
Hello.
ITs hard to say without testing... Only you can solve this. Learn to DEBUGG the code while runnign, see why is activating the part of the code before it should.
Your answer
![](https://koobas.hobune.stream/wayback/20220612230010im_/https://answers.unity.com/themes/thub/images/avi.jpg)