i had a coin magnet in my game, but when my character pass through the 2nd magnet, it won't collect coins, can someone help me? here's my code for the character Code (JavaScript): #pragma strict static var attractCoins: boolean = false; var timer = 15.0; var icon : Texture2D; var collect = 5.0; var isGet : boolean = false; //Invoke("Collect", collect); function Start () { attractCoins = false; } function OnTriggerEnter(info : Collider) { //isGet = true; if(info.tag == "Magnet") { attractCoins = true; } } function Update () { if (attractCoins) { timer -= Time.deltaTime; if (timer <= 0) { timer = 0; attractCoins = false; Collect(); } } } function OnGUI(){ if (attractCoins) { GUI.Box (Rect (10,Screen.height - 120,85,105), "Magnet"); (GUI.Button (Rect (20,Screen.height - 95, 60, 60), icon)); GUI.Label(Rect(25,Screen.height - 35,110,50), "Timer:" + timer.ToString("0")); } } function Collect(){ Debug.Log("collect"); isGet = true; } and here's for coin script Code (JavaScript): #pragma strict var target : GameObject; function Start () { target = GameObject.FindGameObjectWithTag("Player"); } function Update () { if(car.attractCoins == true && Vector3.Distance(transform.position,target.transform.position) < 5.0) { transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Time.deltaTime * 5); } } hope that you'll help me .thanks
Hi joy, I am not that familiar with Javascript, but in looking at the code I was wondering... when should the timer be decreasing itself and how long should the timer run before collecting the coins? Should the timer be reset to 15.0f instead of 0.0f in the Update function? Like, changing this code from this: Code (JavaScript): function Update () { if (attractCoins) { timer -= Time.deltaTime; if (timer <= 0) { timer = 0; attractCoins = false; Collect(); } } } To this: Code (JavaScript): function Update () { if (attractCoins) { timer -= Time.deltaTime; if (timer <= 0) { timer = 15; // Set the timer to the original value. attractCoins = false; Collect(); } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SilverCoin : MonoBehaviour { GameObject[] currentCoins; public bool coinMag = false; public float Timer = 0.0f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (coinMag) { Timer += 1 * Time.deltaTime; if (Timer >= 10) { coinMag = true; Timer = 0; } } GameObject P = GameObject.FindGameObjectWithTag ("Player"); currentCoins = GameObject.FindGameObjectsWithTag("SilverCoin"); GameObject M = GameObject.FindGameObjectWithTag ("SilverMagnet"); foreach (GameObject Coin in currentCoins) { if (Vector3.Distance (Coin.transform.position, P.transform.position) < 0.5) { Destroy (Coin.gameObject); } if (coinMag) { if (Vector3.Distance (Coin.transform.position, P.transform.position) < 5) { Coin.transform.Translate ((P.transform.position - Coin.transform.position).normalized * 1 *Time.deltaTime,Space.World); } } } if (M != null) { if (Vector3.Distance (M.transform.position, P.transform.position) < 0.5) { Debug.Log ("......................................."); Destroy (M.gameObject); if (coinMag == false) { coinMag = true; } } } } }