- Home /
Question by
vjohnree25 · Dec 14, 2018 at 01:18 PM ·
scripting problemcharactermovetowardscoinmagnet
Endless Runner Coin Magnet Not working.
I have two different code for my coin and magnet but the coin didn't move towards the position of the character after the magnet has been pick up. I don't know whats wrong with my script. Here is my script for the magnet.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Magnet : MonoBehaviour {
public static Magnet Instance { set; get; }
public bool coinMag { set; get; }
public float timer = 0.0f;
private Coin coin;
// Update is called once per frame
private void Awake()
{
coinMag = false;
Instance = this;
GameObject P = GameObject.FindGameObjectWithTag("Player");
GameObject M = GameObject.FindGameObjectWithTag("Magnet");
GameObject Coin = GameObject.FindGameObjectWithTag("Coin");
}
private void FixedUpdate()
{
if (coinMag == true)
{
timer += 1 * Time.deltaTime;
if (timer <= 10)
{
coinMag = false;
timer = 0;
}
}
if (coinMag == true)
{
if (Vector3.Distance(GameObject.FindGameObjectWithTag("Coin").transform.position, GameObject.FindGameObjectWithTag("Player").transform.position) < 10)
{
GameObject.FindGameObjectWithTag("Coin").transform.Translate((GameObject.FindGameObjectWithTag("Player").transform.position - GameObject.FindGameObjectWithTag("Coin").transform.position).normalized * 4 * Time.deltaTime, Space.World);
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Destroy(this);
Debug.Log("Magnet has been picked up");
MagnetActive.Instance.Magnetize();
if (!coinMag)
{
coinMag = true;
}
}
}
}
Comment
And here is my script for the coin:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Coin : $$anonymous$$onoBehaviour {
public static Coin Instance { set; get; }
private Animator anim;
private void Awake()
{
Instance = this;
anim = GetComponent<Animator>();
//$$anonymous$$agnetActive();
}
private void OnEnable()
{
anim.SetTrigger("Spawn");
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
Game$$anonymous$$anager.Instance.GetCoin();
anim.SetTrigger("Collected");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612183949im_/https://answers.unity.com/themes/thub/images/avi.jpg)